| 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 | |
| 23 | #define NR_TESTS 60 |
| 24 | |
| 25 | struct test_data { |
| 26 | bt_graph_simple_sink_component_init_func_status init_status; |
| 27 | bt_graph_simple_sink_component_consume_func_status consume_status; |
| 28 | }; |
| 29 | |
| 30 | static |
| 31 | bt_graph_simple_sink_component_init_func_status simple_init_func( |
| 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 | |
| 42 | static |
| 43 | bt_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 | |
| 54 | static |
| 55 | void simple_fini_func(void *data) |
| 56 | { |
| 57 | ok(data, "Data is not NULL in finalization function"); |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | bt_component_class_init_method_status src_init( |
| 62 | bt_self_component_source *self_comp, |
| 63 | const bt_value *params, void *init_method_data) |
| 64 | { |
| 65 | bt_self_component_add_port_status status; |
| 66 | |
| 67 | status = bt_self_component_source_add_output_port(self_comp, |
| 68 | "out", NULL, NULL); |
| 69 | BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); |
| 70 | return BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK; |
| 71 | } |
| 72 | |
| 73 | static |
| 74 | bt_component_class_message_iterator_next_method_status src_iter_next( |
| 75 | bt_self_message_iterator *message_iterator, |
| 76 | bt_message_array_const msgs, uint64_t capacity, |
| 77 | uint64_t *count) |
| 78 | { |
| 79 | return BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; |
| 80 | } |
| 81 | |
| 82 | static |
| 83 | bt_graph *create_graph_with_source(const bt_port_output **out_port) |
| 84 | { |
| 85 | bt_component_class_source *src_comp_cls; |
| 86 | bt_graph *graph; |
| 87 | const bt_component_source *src_comp = NULL; |
| 88 | bt_graph_add_component_status add_comp_status; |
| 89 | bt_component_class_set_method_status set_method_status; |
| 90 | |
| 91 | BT_ASSERT(out_port); |
| 92 | src_comp_cls = bt_component_class_source_create("src", src_iter_next); |
| 93 | BT_ASSERT(src_comp_cls); |
| 94 | set_method_status = bt_component_class_source_set_init_method( |
| 95 | src_comp_cls, src_init); |
| 96 | BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
| 97 | graph = bt_graph_create(0); |
| 98 | BT_ASSERT(graph); |
| 99 | add_comp_status = bt_graph_add_source_component(graph, src_comp_cls, |
| 100 | "src", NULL, BT_LOGGING_LEVEL_NONE, &src_comp); |
| 101 | BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); |
| 102 | BT_ASSERT(src_comp); |
| 103 | *out_port = bt_component_source_borrow_output_port_by_index_const( |
| 104 | src_comp, 0); |
| 105 | BT_ASSERT(*out_port); |
| 106 | bt_component_source_put_ref(src_comp); |
| 107 | bt_component_class_source_put_ref(src_comp_cls); |
| 108 | return graph; |
| 109 | } |
| 110 | |
| 111 | static |
| 112 | void test_simple_expect_run_once_status( |
| 113 | bt_graph_simple_sink_component_init_func_status init_status, |
| 114 | bt_graph_simple_sink_component_consume_func_status consume_status, |
| 115 | bt_graph_run_once_status exp_run_once_status) |
| 116 | { |
| 117 | const bt_port_output *src_out_port = NULL; |
| 118 | bt_graph *graph; |
| 119 | const bt_component_sink *sink_comp = NULL; |
| 120 | const bt_port_input *sink_in_port; |
| 121 | bt_graph_add_component_status add_comp_status; |
| 122 | bt_graph_run_once_status run_once_status; |
| 123 | bt_graph_connect_ports_status connect_status; |
| 124 | struct test_data test_data = { |
| 125 | .init_status = init_status, |
| 126 | .consume_status = consume_status, |
| 127 | }; |
| 128 | |
| 129 | graph = create_graph_with_source(&src_out_port); |
| 130 | BT_ASSERT(graph); |
| 131 | BT_ASSERT(src_out_port); |
| 132 | add_comp_status = bt_graph_add_simple_sink_component(graph, "sink", |
| 133 | simple_init_func, simple_consume_func, simple_fini_func, |
| 134 | &test_data, &sink_comp); |
| 135 | BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); |
| 136 | BT_ASSERT(sink_comp); |
| 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\""); |
| 141 | connect_status = bt_graph_connect_ports(graph, src_out_port, |
| 142 | sink_in_port, NULL); |
| 143 | ok(connect_status == BT_GRAPH_CONNECT_PORTS_STATUS_OK, |
| 144 | "Simple sink component's \"in\" port is connectable"); |
| 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 | bt_component_sink_put_ref(sink_comp); |
| 150 | bt_graph_put_ref(graph); |
| 151 | } |
| 152 | |
| 153 | int main(void) |
| 154 | { |
| 155 | plan_tests(NR_TESTS); |
| 156 | |
| 157 | /* Test initialization function status */ |
| 158 | test_simple_expect_run_once_status( |
| 159 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK, |
| 160 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
| 161 | BT_GRAPH_RUN_ONCE_STATUS_OK); |
| 162 | test_simple_expect_run_once_status( |
| 163 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_ERROR, |
| 164 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
| 165 | BT_GRAPH_RUN_ONCE_STATUS_ERROR); |
| 166 | test_simple_expect_run_once_status( |
| 167 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_MEMORY_ERROR, |
| 168 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
| 169 | BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR); |
| 170 | |
| 171 | /* Test "consume" function status */ |
| 172 | test_simple_expect_run_once_status( |
| 173 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK, |
| 174 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
| 175 | BT_GRAPH_RUN_ONCE_STATUS_OK); |
| 176 | test_simple_expect_run_once_status( |
| 177 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK, |
| 178 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_ERROR, |
| 179 | BT_GRAPH_RUN_ONCE_STATUS_ERROR); |
| 180 | test_simple_expect_run_once_status( |
| 181 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK, |
| 182 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_MEMORY_ERROR, |
| 183 | BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR); |
| 184 | test_simple_expect_run_once_status( |
| 185 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK, |
| 186 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_AGAIN, |
| 187 | BT_GRAPH_RUN_ONCE_STATUS_AGAIN); |
| 188 | test_simple_expect_run_once_status( |
| 189 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK, |
| 190 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_END, |
| 191 | BT_GRAPH_RUN_ONCE_STATUS_END); |
| 192 | |
| 193 | return exit_status(); |
| 194 | } |