tests/lib: create component classes using C++ bindings in `utils/run-in.cpp`
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 14 Feb 2024 17:09:42 +0000 (12:09 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 19 Feb 2024 18:10:15 +0000 (13:10 -0500)
Change-Id: I449799599a5e0d3b1f4ad1864c027b5e60886cda
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11805
Tested-by: jenkins <jenkins@lttng.org>
tests/lib/Makefile.am
tests/lib/utils/run-in.cpp

index 3eed8c8c2b1bfe23805bfe051cbde888f74182b3..edb150d76d8316640b0bc2cb98cbc71a0fdd7867 100644 (file)
@@ -17,7 +17,8 @@ test_bt_values_LDADD = $(COMMON_TEST_LDADD) \
 
 test_fields_bin_SOURCES = test-fields-bin.cpp
 test_fields_bin_LDADD = $(COMMON_TEST_LDADD) \
-       $(top_builddir)/src/lib/libbabeltrace2.la
+       $(top_builddir)/src/lib/libbabeltrace2.la \
+       $(top_builddir)/src/cpp-common/vendor/fmt/libfmt.la
 
 test_bt_uuid_SOURCES = test-bt-uuid.c
 test_bt_uuid_LDADD = $(COMMON_TEST_LDADD)
index 3fef7d327b553f8073596869465c0047ec77e1c5..89e85256067855d19a2293ecfeb7e7d33034488b 100644 (file)
@@ -7,7 +7,8 @@
 #include <utility>
 
 #include "common/assert.h"
-#include "cpp-common/bt2/wrap.hpp"
+#include "cpp-common/bt2/component-class-dev.hpp"
+#include "cpp-common/bt2/component-class.hpp"
 
 #include "run-in.hpp"
 
@@ -20,113 +21,87 @@ struct RunInData final
     RunInMsgIterClsInitFunc msgIterCtxFunc;
 };
 
-const RunInData& runInDataFromMethodData(void * const methodData)
-{
-    return *static_cast<const RunInData *>(methodData);
-}
+class RunInSource;
 
-bt_component_class_initialize_method_status compClsInit(bt_self_component_source * const selfComp,
-                                                        bt_self_component_source_configuration *,
-                                                        const bt_value *,
-                                                        void * const initMethodData)
+class RunInSourceMsgIter final : public bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource>
 {
-    const auto status =
-        bt_self_component_source_add_output_port(selfComp, "out", initMethodData, nullptr);
-
-    BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK);
-
-    auto& data = runInDataFromMethodData(initMethodData);
+public:
+    explicit RunInSourceMsgIter(const bt2::SelfMessageIterator self,
+                                bt2::SelfMessageIteratorConfiguration,
+                                const bt2::SelfComponentOutputPort port) :
+        bt2::UserMessageIterator<RunInSourceMsgIter, RunInSource> {self, "RUN-IN-SRC-MSG-ITER"}
+    {
+        const auto& data = port.data<const RunInData>();
 
-    if (data.compCtxFunc) {
-        data.compCtxFunc(bt2::wrap(bt_self_component_source_as_self_component(selfComp)));
+        if (data.msgIterCtxFunc) {
+            data.msgIterCtxFunc(self);
+        }
     }
 
-    return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
-}
-
-bt_component_class_query_method_status
-compClsQuery(bt_self_component_class_source * const selfCompCls, bt_private_query_executor *,
-             const char *, const bt_value *, void * const methodData,
-             const bt_value ** const result)
-{
-    auto& data = runInDataFromMethodData(methodData);
-
-    if (data.compClsCtxFunc) {
-        data.compClsCtxFunc(
-            bt2::wrap(bt_self_component_class_source_as_self_component_class(selfCompCls)));
+    void _next(bt2::ConstMessageArray&)
+    {
     }
+};
 
-    *result = bt_value_null;
-    return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
-}
-
-bt_message_iterator_class_initialize_method_status
-msgIterClsInit(bt_self_message_iterator * const selfMsgIter,
-               bt_self_message_iterator_configuration *, bt_self_component_port_output * const port)
+class RunInSource final :
+    public bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData,
+                                    const RunInData>
 {
-    auto& data = runInDataFromMethodData(bt_self_component_port_get_data(
-        bt_self_component_port_output_as_self_component_port(port)));
+public:
+    static constexpr auto name = "run-in-src";
+
+    explicit RunInSource(const bt2::SelfSourceComponent self, bt2::ConstMapValue,
+                         const RunInData * const runInData) :
+        bt2::UserSourceComponent<RunInSource, RunInSourceMsgIter, const RunInData,
+                                 const RunInData> {self, "RUN-IN-SRC"},
+        _mRunInData {runInData}
+    {
+        this->_addOutputPort("out", *runInData);
 
-    if (data.msgIterCtxFunc) {
-        data.msgIterCtxFunc(bt2::wrap(selfMsgIter));
+        if (_mRunInData->compCtxFunc) {
+            _mRunInData->compCtxFunc(self);
+        }
     }
 
-    return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
-}
+    static bt2::Value::Shared _query(const bt2::SelfComponentClass self, bt2::PrivateQueryExecutor,
+                                     bt2c::CStringView, bt2::ConstValue,
+                                     const RunInData * const data)
+    {
+        if (data->compClsCtxFunc) {
+            data->compClsCtxFunc(self);
+        }
 
-bt_message_iterator_class_next_method_status
-msgIterClsNext(bt_self_message_iterator *, bt_message_array_const, uint64_t, uint64_t *)
-{
-    return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
-}
+        return bt2::NullValue {}.shared();
+    }
 
-struct DummySinkData
-{
-    bt_message_iterator *msgIter;
+private:
+    const RunInData *_mRunInData;
 };
 
-bt_component_class_initialize_method_status dummySinkInit(bt_self_component_sink * const self,
-                                                          bt_self_component_sink_configuration *,
-                                                          const bt_value *,
-                                                          void * const initMethodData)
-{
-    const auto status = bt_self_component_sink_add_input_port(self, "in", NULL, nullptr);
-
-    BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK);
-    bt_self_component_set_data(bt_self_component_sink_as_self_component(self), initMethodData);
-    return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
-}
-
-DummySinkData& dummySinkDataFromSelfCompSink(bt_self_component_sink * const self)
+class DummySink : public bt2::UserSinkComponent<DummySink>
 {
-    return *static_cast<DummySinkData *>(
-        bt_self_component_get_data(bt_self_component_sink_as_self_component(self)));
-}
-
-bt_component_class_sink_graph_is_configured_method_status
-dummySinkGraphIsConfigured(bt_self_component_sink * const self)
-{
-    const auto port = bt_self_component_sink_borrow_input_port_by_name(self, "in");
-
-    BT_ASSERT(port);
+public:
+    static constexpr auto name = "dummy";
 
-    const auto status = bt_message_iterator_create_from_sink_component(
-        self, port, &dummySinkDataFromSelfCompSink(self).msgIter);
+    explicit DummySink(const bt2::SelfSinkComponent self, bt2::ConstMapValue, void *) :
+        bt2::UserSinkComponent<DummySink>(self, "DUMMY-SINK")
+    {
+        this->_addInputPort("in");
+    }
 
-    BT_ASSERT(status == BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK);
-    return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
-}
+    void _graphIsConfigured()
+    {
+        _mMsgIter = this->_createMessageIterator(this->_inputPorts()["in"]);
+    }
 
-bt_component_class_sink_consume_method_status dummySinkConsume(bt_self_component_sink * const self)
-{
-    bt_message_array_const msgs;
-    uint64_t msgCount;
-    const auto status =
-        bt_message_iterator_next(dummySinkDataFromSelfCompSink(self).msgIter, &msgs, &msgCount);
+    bool _consume()
+    {
+        return _mMsgIter->next().has_value();
+    }
 
-    BT_ASSERT(status == BT_MESSAGE_ITERATOR_NEXT_STATUS_END);
-    return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
-}
+private:
+    bt2::MessageIterator::Shared _mMsgIter;
+};
 
 } /* namespace */
 
@@ -134,40 +109,13 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun
            RunInMsgIterClsInitFunc msgIterCtxFunc)
 {
     RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)};
-
-    /* Create and configure custom source component class */
-    const auto msgIterCls = bt_message_iterator_class_create(msgIterClsNext);
-
-    BT_ASSERT(msgIterCls);
-
-    {
-        const auto status =
-            bt_message_iterator_class_set_initialize_method(msgIterCls, msgIterClsInit);
-
-        BT_ASSERT(status == BT_MESSAGE_ITERATOR_CLASS_SET_METHOD_STATUS_OK);
-    }
-
-    const auto srcCompCls = bt_component_class_source_create("yo", msgIterCls);
-
-    BT_ASSERT(srcCompCls);
-
-    {
-        const auto status =
-            bt_component_class_source_set_initialize_method(srcCompCls, compClsInit);
-
-        BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
-    }
-
-    {
-        const auto status = bt_component_class_source_set_query_method(srcCompCls, compClsQuery);
-
-        BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
-    }
+    const auto srcCompCls = bt2::SourceComponentClass::create<RunInSource>();
 
     /* Execute a query (executes `compClsCtxFunc`) */
     {
         const auto queryExec = bt_query_executor_create_with_method_data(
-            bt_component_class_source_as_component_class(srcCompCls), "", nullptr, &data);
+            bt_component_class_source_as_component_class(srcCompCls->libObjPtr()), "", nullptr,
+            &data);
 
         BT_ASSERT(queryExec);
 
@@ -179,25 +127,6 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun
         bt_query_executor_put_ref(queryExec);
     }
 
-    /* Create a dummy sink component */
-    const auto sinkCompCls = bt_component_class_sink_create("dummy", dummySinkConsume);
-
-    BT_ASSERT(sinkCompCls);
-
-    {
-        const auto status =
-            bt_component_class_sink_set_initialize_method(sinkCompCls, dummySinkInit);
-
-        BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
-    }
-
-    {
-        const auto status = bt_component_class_sink_set_graph_is_configured_method(
-            sinkCompCls, dummySinkGraphIsConfigured);
-
-        BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
-    }
-
     /* Create graph */
     const auto graph = bt_graph_create(0);
 
@@ -208,18 +137,20 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun
 
     {
         const auto status = bt_graph_add_source_component_with_initialize_method_data(
-            graph, srcCompCls, "the-source", NULL, &data, BT_LOGGING_LEVEL_NONE, &srcComp);
+            graph, srcCompCls->libObjPtr(), "the-source", NULL, &data, BT_LOGGING_LEVEL_NONE,
+            &srcComp);
 
         BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
     }
 
     /* Add dummy sink component */
     const bt_component_sink *sinkComp;
-    DummySinkData dummySinkData;
 
     {
+        const auto sinkCompCls = bt2::SinkComponentClass::create<DummySink>();
         const auto status = bt_graph_add_sink_component_with_initialize_method_data(
-            graph, sinkCompCls, "the-sink", NULL, &dummySinkData, BT_LOGGING_LEVEL_NONE, &sinkComp);
+            graph, sinkCompCls->libObjPtr(), "the-sink", nullptr, nullptr, BT_LOGGING_LEVEL_NONE,
+            &sinkComp);
 
         BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
     }
@@ -248,9 +179,6 @@ void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFun
 
     /* Discard owned objects */
     bt_graph_put_ref(graph);
-    bt_component_class_source_put_ref(srcCompCls);
-    bt_component_class_sink_put_ref(sinkCompCls);
-    bt_message_iterator_class_put_ref(msgIterCls);
 }
 
 void runInCompClsQuery(RunInCompClsQueryFunc func)
This page took 0.02901 seconds and 4 git commands to generate.