Add bt_graph_add_simple_sink_component() tests
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 5 Aug 2019 22:01:09 +0000 (18:01 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 6 Aug 2019 20:17:13 +0000 (16:17 -0400)
As of this patch, the tests are basic and only check that the statuses
returned by the user functions are correctly translated and returned by
bt_graph_run_once(). The user initialization function status is not
returned by bt_graph_add_simple_sink_component() because the
initialization function is called within the sink component's "graph is
configured" method.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Id8ccfd7df4e4a8f0a39af386ed4ad13fe657f86a
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1830
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
.gitignore
tests/Makefile.am
tests/lib/Makefile.am
tests/lib/test_simple_sink.c [new file with mode: 0644]

index 2141ebfc93c8994b551351519f5d89c36ae56509..314dfde36bfeeadda64f50b335aa8e98cd0fabba 100644 (file)
@@ -6,6 +6,7 @@
 /tests/lib/test_bt_values
 /tests/lib/test_graph_topo
 /tests/lib/test_trace_ir_ref
+/tests/lib/test_simple_sink
 /tests/plugins/flt.lttng-utils.debug-info/test_bin_info
 /tests/plugins/flt.lttng-utils.debug-info/test_dwarf
 /tests/plugins/src.ctf.fs/succeed/gen-trace-simple
index 5b641c8d469fba9571d59127af50aa63ab9fda0e..aa48914a8ce6999b51f482fbdfd661fa76bb0167 100644 (file)
@@ -64,7 +64,8 @@ TESTS_LIB = \
        lib/test_bt_values \
        lib/test_bt_uuid \
        lib/test_graph_topo \
-       lib/test_trace_ir_ref
+       lib/test_trace_ir_ref \
+       lib/test_simple_sink
 
 TESTS_BITFIELD = \
        bitfield/test_bitfield
index 8974725c9eae16a788991b551063bc2b34f4c067..67650cd52892febedf288a2b944e94c1a9e89109 100644 (file)
@@ -20,9 +20,18 @@ test_trace_ir_ref_LDADD = $(COMMON_TEST_LDADD) \
 test_graph_topo_LDADD = $(COMMON_TEST_LDADD) \
        $(top_builddir)/src/lib/libbabeltrace2.la
 
-noinst_PROGRAMS = test_bt_values test_bt_uuid test_trace_ir_ref test_graph_topo
+test_simple_sink_LDADD = $(COMMON_TEST_LDADD) \
+       $(top_builddir)/src/lib/libbabeltrace2.la
+
+noinst_PROGRAMS = \
+       test_bt_values \
+       test_bt_uuid \
+       test_trace_ir_ref \
+       test_graph_topo \
+       test_simple_sink
 
 test_bt_values_SOURCES = test_bt_values.c
+test_simple_sink_SOURCES = test_simple_sink.c
 test_bt_uuid_SOURCES = test_bt_uuid.c
 test_trace_ir_ref_SOURCES = test_trace_ir_ref.c
 test_graph_topo_SOURCES = test_graph_topo.c
diff --git a/tests/lib/test_simple_sink.c b/tests/lib/test_simple_sink.c
new file mode 100644 (file)
index 0000000..f0599bb
--- /dev/null
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <babeltrace2/babeltrace.h>
+#include "common/assert.h"
+#include <string.h>
+#include "tap/tap.h"
+
+#define NR_TESTS 60
+
+struct test_data {
+       bt_graph_simple_sink_component_init_func_status init_status;
+       bt_graph_simple_sink_component_consume_func_status consume_status;
+};
+
+static
+bt_graph_simple_sink_component_init_func_status simple_init_func(
+               bt_self_component_port_input_message_iterator *iterator,
+               void *data)
+{
+       struct test_data *test_data = data;
+
+       ok(iterator, "Message iterator is not NULL in initialization function");
+       ok(data, "Data is not NULL in initialization function");
+       return test_data->init_status;
+}
+
+static
+bt_graph_simple_sink_component_consume_func_status simple_consume_func(
+               bt_self_component_port_input_message_iterator *iterator,
+               void *data)
+{
+       struct test_data *test_data = data;
+
+       ok(iterator, "Message iterator is not NULL in consume function");
+       ok(data, "Data is not NULL in consume function");
+       return test_data->consume_status;
+}
+
+static
+void simple_fini_func(void *data)
+{
+       ok(data, "Data is not NULL in finalization function");
+}
+
+static
+bt_component_class_init_method_status src_init(
+               bt_self_component_source *self_comp,
+               const bt_value *params, void *init_method_data)
+{
+       bt_self_component_add_port_status status;
+
+       status = bt_self_component_source_add_output_port(self_comp,
+               "out", NULL, NULL);
+       BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK);
+       return BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
+}
+
+static
+bt_component_class_message_iterator_next_method_status src_iter_next(
+               bt_self_message_iterator *message_iterator,
+               bt_message_array_const msgs, uint64_t capacity,
+               uint64_t *count)
+{
+       return BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
+}
+
+static
+bt_graph *create_graph_with_source(const bt_port_output **out_port)
+{
+       bt_component_class_source *src_comp_cls;
+       bt_graph *graph;
+       const bt_component_source *src_comp = NULL;
+       bt_graph_add_component_status add_comp_status;
+       bt_component_class_set_method_status set_method_status;
+
+       BT_ASSERT(out_port);
+       src_comp_cls = bt_component_class_source_create("src", src_iter_next);
+       BT_ASSERT(src_comp_cls);
+       set_method_status = bt_component_class_source_set_init_method(
+               src_comp_cls, src_init);
+       BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
+       graph = bt_graph_create();
+       BT_ASSERT(graph);
+       add_comp_status = bt_graph_add_source_component(graph, src_comp_cls,
+               "src", NULL, BT_LOGGING_LEVEL_NONE, &src_comp);
+       BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
+       BT_ASSERT(src_comp);
+       *out_port = bt_component_source_borrow_output_port_by_index_const(
+               src_comp, 0);
+       BT_ASSERT(*out_port);
+       bt_component_source_put_ref(src_comp);
+       bt_component_class_source_put_ref(src_comp_cls);
+       return graph;
+}
+
+static
+void test_simple_expect_run_once_status(
+               bt_graph_simple_sink_component_init_func_status init_status,
+               bt_graph_simple_sink_component_consume_func_status consume_status,
+               bt_graph_run_once_status exp_run_once_status)
+{
+       const bt_port_output *src_out_port = NULL;
+       bt_graph *graph;
+       const bt_component_sink *sink_comp = NULL;
+       const bt_port_input *sink_in_port;
+       bt_graph_add_component_status add_comp_status;
+       bt_graph_run_once_status run_once_status;
+       bt_graph_connect_ports_status connect_status;
+       struct test_data test_data = {
+               .init_status = init_status,
+               .consume_status = consume_status,
+       };
+
+       graph = create_graph_with_source(&src_out_port);
+       BT_ASSERT(graph);
+       BT_ASSERT(src_out_port);
+       add_comp_status = bt_graph_add_simple_sink_component(graph, "sink",
+               simple_init_func, simple_consume_func, simple_fini_func,
+               &test_data, &sink_comp);
+       BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
+       BT_ASSERT(sink_comp);
+       sink_in_port = bt_component_sink_borrow_input_port_by_name_const(
+               sink_comp, "in");
+       ok(sink_in_port,
+               "Simple sink component has an input port named \"in\"");
+       connect_status = bt_graph_connect_ports(graph, src_out_port,
+               sink_in_port, NULL);
+       ok(connect_status == BT_GRAPH_CONNECT_PORTS_STATUS_OK,
+               "Simple sink component's \"in\" port is connectable");
+       run_once_status = bt_graph_run_once(graph);
+       ok(run_once_status == exp_run_once_status,
+               "Graph \"run once\" status is the expected one (status code: %d)",
+               run_once_status);
+       bt_component_sink_put_ref(sink_comp);
+       bt_graph_put_ref(graph);
+}
+
+int main(void)
+{
+       plan_tests(NR_TESTS);
+
+       /* Test initialization function status */
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
+               BT_GRAPH_RUN_ONCE_STATUS_OK);
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_ERROR,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
+               BT_GRAPH_RUN_ONCE_STATUS_ERROR);
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_MEMORY_ERROR,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
+               BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR);
+
+       /* Test "consume" function status */
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
+               BT_GRAPH_RUN_ONCE_STATUS_OK);
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_ERROR,
+               BT_GRAPH_RUN_ONCE_STATUS_ERROR);
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_MEMORY_ERROR,
+               BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR);
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_AGAIN,
+               BT_GRAPH_RUN_ONCE_STATUS_AGAIN);
+       test_simple_expect_run_once_status(
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_INIT_FUNC_STATUS_OK,
+               BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_END,
+               BT_GRAPH_RUN_ONCE_STATUS_END);
+
+       return exit_status();
+}
This page took 0.02808 seconds and 4 git commands to generate.