#define BOOST_TEST_DYN_LINK #include "core/sophiar_manager.h" #include "core/tristate_obj.h" #include "core/basic_obj_types.hpp" #include "utility/debug_utility.hpp" #include "utility/global_obj_helper.hpp" #include #include #include #include #include #include #include #include #include using boost::asio::awaitable; using boost::asio::co_spawn; using boost::asio::detached; using boost::asio::use_awaitable; using namespace sophiar; using namespace std::chrono_literals; struct source_node_type : public tristate_obj { DEFAULT_NEW_INSTANCE(source_node_type) global_obj_auto_sync_delegate out_obj; boost::asio::awaitable on_init(const nlohmann::json &config) override { init_value = config["output_obj_name"].get(); co_return true; // TODO } boost::asio::awaitable on_start(const nlohmann::json &config) override { assert(config.contains("start_value")); assert(config["start_value"].is_number()); start_value = config["start_value"].get(); co_spawn(get_context(), [=]() -> awaitable { co_await coro_sleep(50ms); this->output_signal.emit(init_value * start_value); }, detached); co_return true; } using output_signal_type = tiny_signal; output_signal_type output_signal; int64_t init_value = -1, start_value = -1; }; struct proxy_node_type : public tristate_obj { DEFAULT_NEW_INSTANCE(proxy_node_type); using signal_type = tiny_signal; using slot_type = signal_type::slot_type; struct slot_impl_type : public slot_type { proxy_node_type *p_this = nullptr; void on_signal_received(int64_t args) override { assert(p_this != nullptr); p_this->output_signal.emit(args * p_this->init_value); } }; void load_construct_config(const nlohmann::json &config) override { BOOST_TEST(config.empty()); auto input_slot = new slot_impl_type(); input_slot->p_this = this; get_manager().register_signal(this, "output", output_signal); get_manager().register_slot(this, "input", *input_slot); }; boost::asio::awaitable on_init(const nlohmann::json &config) override { assert(config.contains("init_value")); assert(config["init_value"].is_number()); init_value = config["init_value"].get(); co_return true; } boost::asio::awaitable on_start(const nlohmann::json &config) override { BOOST_TEST(config.empty()); co_spawn(get_context(), [=]() -> awaitable { co_await coro_sleep(100ms); co_await this->stop(); }, detached); co_return true; } signal_type output_signal; int64_t init_value = -1; }; struct target_node_type : public tristate_obj { DEFAULT_NEW_INSTANCE(target_node_type); using signal_type = tiny_signal; using slot_type = signal_type::slot_type; struct slot_impl_type : public slot_type { void on_signal_received(int64_t args) override { std::cout << args << std::endl; } }; void load_construct_config(const nlohmann::json &config) override { BOOST_TEST(config.empty()); get_manager().register_slot(this, "input", *(new slot_impl_type)); }; boost::asio::awaitable on_init(const nlohmann::json &config) override { BOOST_TEST(config.empty()); co_return true; } boost::asio::awaitable on_start(const nlohmann::json &config) override { BOOST_TEST(config.empty()); co_return true; } }; BOOST_AUTO_TEST_CASE(test_sophiar_manager) { spdlog::set_level(spdlog::level::trace); REGISTER_TYPE(source_node_type); REGISTER_TYPE(proxy_node_type); REGISTER_TYPE(target_node_type); std::ifstream config_file("data/sophiar_manager_config.json"); BOOST_TEST(config_file.is_open()); global_sophiar_manager.load_config_and_start(nlohmann::json::parse(config_file)); co_spawn(global_context, []() -> awaitable { co_await global_sophiar_manager.switch_mode("mode_a"); co_await coro_sleep(100ms); co_await global_sophiar_manager.switch_mode("mode_b"); // co_await coro_sleep(100ms); // co_await global_sophiar_manager.switch_mode("mode_a"); }, detached); global_context.run(); }