From afbf4b1a2d797ed72b1f24a8fce7b0c7e611a470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 11 Jan 2025 18:33:51 +0100 Subject: [PATCH] Test channel --- nano/lib/observer_set.hpp | 6 ++++ nano/node/CMakeLists.txt | 2 ++ nano/node/transport/test_channel.cpp | 19 ++++++++++++ nano/node/transport/test_channel.hpp | 45 ++++++++++++++++++++++++++++ nano/test_common/testutil.cpp | 10 +++++++ nano/test_common/testutil.hpp | 5 ++++ 6 files changed, 87 insertions(+) create mode 100644 nano/node/transport/test_channel.cpp create mode 100644 nano/node/transport/test_channel.hpp diff --git a/nano/lib/observer_set.hpp b/nano/lib/observer_set.hpp index 5630515fa..b0048bdb2 100644 --- a/nano/lib/observer_set.hpp +++ b/nano/lib/observer_set.hpp @@ -46,6 +46,12 @@ public: return observers.size (); } + void clear () + { + nano::lock_guard lock{ mutex }; + observers.clear (); + } + nano::container_info container_info () const { nano::unique_lock lock{ mutex }; diff --git a/nano/node/CMakeLists.txt b/nano/node/CMakeLists.txt index 77eb01e82..396fe33cd 100644 --- a/nano/node/CMakeLists.txt +++ b/nano/node/CMakeLists.txt @@ -174,6 +174,8 @@ add_library( transport/tcp_server.cpp transport/tcp_socket.hpp transport/tcp_socket.cpp + transport/test_channel.hpp + transport/test_channel.cpp transport/traffic_type.hpp transport/traffic_type.cpp transport/transport.hpp diff --git a/nano/node/transport/test_channel.cpp b/nano/node/transport/test_channel.cpp new file mode 100644 index 000000000..743c0e249 --- /dev/null +++ b/nano/node/transport/test_channel.cpp @@ -0,0 +1,19 @@ +#include +#include + +nano::transport::test_channel::test_channel (nano::node & node_a) : + channel (node_a) +{ +} + +bool nano::transport::test_channel::send_impl (nano::message const & message, nano::transport::traffic_type traffic_type, callback_t callback) +{ + observers.notify (message, traffic_type); + + if (callback) + { + callback (boost::system::errc::make_error_code (boost::system::errc::success), message.to_shared_const_buffer ().size ()); + } + + return true; +} \ No newline at end of file diff --git a/nano/node/transport/test_channel.hpp b/nano/node/transport/test_channel.hpp new file mode 100644 index 000000000..f1e3b50a4 --- /dev/null +++ b/nano/node/transport/test_channel.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +namespace nano::transport +{ +class test_channel final : public nano::transport::channel +{ +public: + nano::observer_set observers; // Called for each queued message + +public: + explicit test_channel (nano::node &); + + nano::endpoint get_remote_endpoint () const override + { + return {}; + } + + nano::endpoint get_local_endpoint () const override + { + return {}; + } + + nano::transport::transport_type get_type () const override + { + return nano::transport::transport_type::loopback; + } + + void close () override + { + // Can't be closed + } + + std::string to_string () const override + { + return "test_channel"; + } + +protected: + bool send_impl (nano::message const &, nano::transport::traffic_type, callback_t) override; +}; +} \ No newline at end of file diff --git a/nano/test_common/testutil.cpp b/nano/test_common/testutil.cpp index a3400f9b9..e463850bf 100644 --- a/nano/test_common/testutil.cpp +++ b/nano/test_common/testutil.cpp @@ -259,6 +259,16 @@ std::shared_ptr nano::test::fake_channel (nano:: return channel; } +std::shared_ptr nano::test::test_channel (nano::node & node, nano::account node_id) +{ + auto channel = std::make_shared (node); + if (!node_id.is_zero ()) + { + channel->set_node_id (node_id); + } + return channel; +} + std::shared_ptr nano::test::start_election (nano::test::system & system_a, nano::node & node_a, const nano::block_hash & hash_a) { system_a.deadline_set (5s); diff --git a/nano/test_common/testutil.hpp b/nano/test_common/testutil.hpp index 1964af057..3f5e0ffdb 100644 --- a/nano/test_common/testutil.hpp +++ b/nano/test_common/testutil.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -385,6 +386,10 @@ namespace test * Creates a new fake channel associated with `node` */ std::shared_ptr fake_channel (nano::node & node, nano::account node_id = { 0 }); + /* + * Creates a new test channel associated with `node` + */ + std::shared_ptr test_channel (nano::node & node, nano::account node_id = { 0 }); /* * Start an election on system system_a, node node_a and hash hash_a by reading the block * out of the ledger and adding it to the manual election scheduler queue.