2 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
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.
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.
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.
18 #include <babeltrace2/babeltrace.h>
19 #include "common/assert.h"
26 bt_graph_simple_sink_component_initialize_func_status init_status
;
27 bt_graph_simple_sink_component_consume_func_status consume_status
;
31 bt_graph_simple_sink_component_initialize_func_status
simple_INITIALIZE_func(
32 bt_message_iterator
*iterator
,
35 struct test_data
*test_data
= data
;
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
;
43 bt_graph_simple_sink_component_consume_func_status
simple_consume_func(
44 bt_message_iterator
*iterator
,
47 struct test_data
*test_data
= data
;
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
;
55 void simple_fini_func(void *data
)
57 ok(data
, "Data is not NULL in finalization function");
61 bt_component_class_initialize_method_status
src_init(
62 bt_self_component_source
*self_comp
,
63 bt_self_component_source_configuration
*config
,
64 const bt_value
*params
, void *init_method_data
)
66 bt_self_component_add_port_status status
;
68 status
= bt_self_component_source_add_output_port(self_comp
,
70 BT_ASSERT(status
== BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
);
71 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
75 bt_message_iterator_class_next_method_status
src_iter_next(
76 bt_self_message_iterator
*message_iterator
,
77 bt_message_array_const msgs
, uint64_t capacity
,
80 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
84 bt_graph
*create_graph_with_source(const bt_port_output
**out_port
)
86 bt_message_iterator_class
*msg_iter_cls
;
87 bt_component_class_source
*src_comp_cls
;
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
;
95 msg_iter_cls
= bt_message_iterator_class_create(src_iter_next
);
96 BT_ASSERT(msg_iter_cls
);
98 src_comp_cls
= bt_component_class_source_create("src", msg_iter_cls
);
99 BT_ASSERT(src_comp_cls
);
100 set_method_status
= bt_component_class_source_set_initialize_method(
101 src_comp_cls
, src_init
);
102 BT_ASSERT(set_method_status
== BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK
);
103 graph
= bt_graph_create(0);
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
);
109 *out_port
= bt_component_source_borrow_output_port_by_index_const(
111 BT_ASSERT(*out_port
);
112 bt_component_class_source_put_ref(src_comp_cls
);
113 bt_message_iterator_class_put_ref(msg_iter_cls
);
118 void test_simple_expect_run_once_status(
119 bt_graph_simple_sink_component_initialize_func_status init_status
,
120 bt_graph_simple_sink_component_consume_func_status consume_status
,
121 bt_graph_run_once_status exp_run_once_status
)
123 const bt_port_output
*src_out_port
= NULL
;
125 const bt_component_sink
*sink_comp
= NULL
;
126 const bt_port_input
*sink_in_port
;
127 bt_graph_add_component_status add_comp_status
;
128 bt_graph_run_once_status run_once_status
;
129 bt_graph_connect_ports_status connect_status
;
130 struct test_data test_data
= {
131 .init_status
= init_status
,
132 .consume_status
= consume_status
,
134 const struct bt_error
*err
;
136 graph
= create_graph_with_source(&src_out_port
);
138 BT_ASSERT(src_out_port
);
140 add_comp_status
= bt_graph_add_simple_sink_component(graph
, "sink",
141 simple_INITIALIZE_func
, simple_consume_func
, simple_fini_func
,
142 &test_data
, &sink_comp
);
143 BT_ASSERT(add_comp_status
== BT_GRAPH_ADD_COMPONENT_STATUS_OK
);
144 BT_ASSERT(sink_comp
);
146 sink_in_port
= bt_component_sink_borrow_input_port_by_name_const(
149 "Simple sink component has an input port named \"in\"");
151 connect_status
= bt_graph_connect_ports(graph
, src_out_port
,
153 ok(connect_status
== BT_GRAPH_CONNECT_PORTS_STATUS_OK
,
154 "Simple sink component's \"in\" port is connectable");
156 run_once_status
= bt_graph_run_once(graph
);
157 ok(run_once_status
== exp_run_once_status
,
158 "Graph \"run once\" status is the expected one (status code: %d)",
161 err
= bt_current_thread_take_error();
162 ok((run_once_status
< 0) == (err
!= NULL
),
163 "Current thread error is set if bt_graph_run_once returned an error");
165 bt_graph_put_ref(graph
);
167 bt_error_release(err
);
173 plan_tests(NR_TESTS
);
175 /* Test initialization function status */
176 test_simple_expect_run_once_status(
177 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK
,
178 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK
,
179 BT_GRAPH_RUN_ONCE_STATUS_OK
);
180 test_simple_expect_run_once_status(
181 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_ERROR
,
182 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK
,
183 BT_GRAPH_RUN_ONCE_STATUS_ERROR
);
184 test_simple_expect_run_once_status(
185 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_MEMORY_ERROR
,
186 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK
,
187 BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR
);
189 /* Test "consume" function status */
190 test_simple_expect_run_once_status(
191 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK
,
192 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK
,
193 BT_GRAPH_RUN_ONCE_STATUS_OK
);
194 test_simple_expect_run_once_status(
195 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK
,
196 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_ERROR
,
197 BT_GRAPH_RUN_ONCE_STATUS_ERROR
);
198 test_simple_expect_run_once_status(
199 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK
,
200 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_MEMORY_ERROR
,
201 BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR
);
202 test_simple_expect_run_once_status(
203 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK
,
204 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_AGAIN
,
205 BT_GRAPH_RUN_ONCE_STATUS_AGAIN
);
206 test_simple_expect_run_once_status(
207 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK
,
208 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_END
,
209 BT_GRAPH_RUN_ONCE_STATUS_END
);
211 return exit_status();