Test channel

This commit is contained in:
Piotr Wójcik 2025-01-11 18:33:51 +01:00
commit afbf4b1a2d
6 changed files with 87 additions and 0 deletions

View file

@ -46,6 +46,12 @@ public:
return observers.size ();
}
void clear ()
{
nano::lock_guard<nano::mutex> lock{ mutex };
observers.clear ();
}
nano::container_info container_info () const
{
nano::unique_lock<nano::mutex> lock{ mutex };

View file

@ -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

View file

@ -0,0 +1,19 @@
#include <nano/node/node.hpp>
#include <nano/node/transport/test_channel.hpp>
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;
}

View file

@ -0,0 +1,45 @@
#pragma once
#include <nano/lib/observer_set.hpp>
#include <nano/node/transport/channel.hpp>
#include <nano/node/transport/fwd.hpp>
namespace nano::transport
{
class test_channel final : public nano::transport::channel
{
public:
nano::observer_set<nano::message, nano::transport::traffic_type> 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;
};
}

View file

@ -259,6 +259,16 @@ std::shared_ptr<nano::transport::fake::channel> nano::test::fake_channel (nano::
return channel;
}
std::shared_ptr<nano::transport::test_channel> nano::test::test_channel (nano::node & node, nano::account node_id)
{
auto channel = std::make_shared<nano::transport::test_channel> (node);
if (!node_id.is_zero ())
{
channel->set_node_id (node_id);
}
return channel;
}
std::shared_ptr<nano::election> nano::test::start_election (nano::test::system & system_a, nano::node & node_a, const nano::block_hash & hash_a)
{
system_a.deadline_set (5s);

View file

@ -4,6 +4,7 @@
#include <nano/lib/timer.hpp>
#include <nano/node/fwd.hpp>
#include <nano/node/transport/fwd.hpp>
#include <nano/node/transport/test_channel.hpp>
#include <nano/secure/account_info.hpp>
#include <nano/store/fwd.hpp>
@ -385,6 +386,10 @@ namespace test
* Creates a new fake channel associated with `node`
*/
std::shared_ptr<nano::transport::fake::channel> fake_channel (nano::node & node, nano::account node_id = { 0 });
/*
* Creates a new test channel associated with `node`
*/
std::shared_ptr<nano::transport::test_channel> 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.