Skip to content

Commit

Permalink
SYSTEM IO TYPES
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 3, 2024
1 parent 59d9065 commit 59c3dd2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 42 deletions.
20 changes: 9 additions & 11 deletions nano/core_test/bootstrap_ascending.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ TEST (account_sets, saturate_priority)
*/
TEST (bootstrap_ascending, account_base)
{
nano::node_flags flags;
nano::test::system system{ 1, nano::transport::transport_type::tcp, flags };
auto & node0 = *system.nodes[0];
nano::test::system system;
auto & node0 = *system.add_node ();
nano::state_block_builder builder;
auto send1 = builder.make_block ()
.account (nano::dev::genesis_key.pub)
Expand All @@ -174,7 +173,7 @@ TEST (bootstrap_ascending, account_base)
.work (*system.work.generate (nano::dev::genesis->hash ()))
.build ();
ASSERT_EQ (nano::block_status::progress, node0.process (send1));
auto & node1 = *system.add_node (flags);
auto & node1 = *system.add_node ();
ASSERT_TIMELY (5s, node1.block (send1->hash ()) != nullptr);
}

Expand All @@ -183,9 +182,8 @@ TEST (bootstrap_ascending, account_base)
*/
TEST (bootstrap_ascending, account_inductive)
{
nano::node_flags flags;
nano::test::system system{ 1, nano::transport::transport_type::tcp, flags };
auto & node0 = *system.nodes[0];
nano::test::system system;
auto & node0 = *system.add_node ();
nano::state_block_builder builder;
auto send1 = builder.make_block ()
.account (nano::dev::genesis_key.pub)
Expand All @@ -210,7 +208,7 @@ TEST (bootstrap_ascending, account_inductive)
// std::cerr << "Send2: " << send2->hash ().to_string () << std::endl;
ASSERT_EQ (nano::block_status::progress, node0.process (send1));
ASSERT_EQ (nano::block_status::progress, node0.process (send2));
auto & node1 = *system.add_node (flags);
auto & node1 = *system.add_node ();
ASSERT_TIMELY (50s, node1.block (send2->hash ()) != nullptr);
}

Expand All @@ -219,10 +217,10 @@ TEST (bootstrap_ascending, account_inductive)
*/
TEST (bootstrap_ascending, trace_base)
{
nano::test::system system;
nano::node_flags flags;
flags.disable_legacy_bootstrap = true;
nano::test::system system{ 1, nano::transport::transport_type::tcp, flags };
auto & node0 = *system.nodes[0];
auto & node0 = *system.add_node (flags);
nano::keypair key;
nano::state_block_builder builder;
auto send1 = builder.make_block ()
Expand All @@ -248,7 +246,7 @@ TEST (bootstrap_ascending, trace_base)
// std::cerr << "Genesis: " << nano::dev::genesis->hash ().to_string () << std::endl;
// std::cerr << "send1: " << send1->hash ().to_string () << std::endl;
// std::cerr << "receive1: " << receive1->hash ().to_string () << std::endl;
auto & node1 = *system.add_node ();
auto & node1 = *system.add_node (flags);
// std::cerr << "--------------- Start ---------------\n";
ASSERT_EQ (nano::block_status::progress, node0.process (send1));
ASSERT_EQ (nano::block_status::progress, node0.process (receive1));
Expand Down
12 changes: 3 additions & 9 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ TEST (network, send_invalid_publish)

TEST (network, send_valid_confirm_ack)
{
auto type = nano::transport::transport_type::tcp;
nano::node_flags node_flags;
nano::test::system system (2, type, node_flags);
nano::test::system system (2);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
nano::keypair key2;
Expand All @@ -258,9 +256,7 @@ TEST (network, send_valid_confirm_ack)

TEST (network, send_valid_publish)
{
auto type = nano::transport::transport_type::tcp;
nano::node_flags node_flags;
nano::test::system system (2, type, node_flags);
nano::test::system system (2);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
node1.bootstrap_initiator.stop ();
Expand Down Expand Up @@ -401,9 +397,7 @@ TEST (receivable_processor, confirm_sufficient_pos)

TEST (receivable_processor, send_with_receive)
{
auto type = nano::transport::transport_type::tcp;
nano::node_flags node_flags;
nano::test::system system (2, type, node_flags);
nano::test::system system (2);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
auto amount (std::numeric_limits<nano::uint128_t>::max ());
Expand Down
7 changes: 2 additions & 5 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ TEST (node, work_generate)
TEST (node, block_store_path_failure)
{
nano::test::system system;
auto io_ctx = std::make_shared<boost::asio::io_context> ();
auto path (nano::unique_path ());
nano::work_pool pool{ nano::dev::network_params.network, std::numeric_limits<unsigned>::max () };
auto node (std::make_shared<nano::node> (io_ctx, system.get_available_port (), path, pool));
auto node (std::make_shared<nano::node> (system.io_ctx, system.get_available_port (), path, pool));
system.register_node (node);
ASSERT_TRUE (node->wallets.items.empty ());
}
Expand Down Expand Up @@ -1809,9 +1808,7 @@ TEST (node, online_reps_election)

TEST (node, block_confirm)
{
auto type = nano::transport::transport_type::tcp;
nano::node_flags node_flags;
nano::test::system system (2, type, node_flags);
nano::test::system system (2);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
nano::keypair key;
Expand Down
4 changes: 3 additions & 1 deletion nano/lib/thread_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace nano
{
using io_guard = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;

class thread_runner final
{
nano::logger logger;
Expand All @@ -26,7 +28,7 @@ class thread_runner final

private:
std::shared_ptr<boost::asio::io_context> io_ctx;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> io_guard;
nano::io_guard io_guard;
nano::thread_role::name const role;
std::vector<boost::thread> threads;

Expand Down
61 changes: 48 additions & 13 deletions nano/test_common/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ std::string nano::error_system_messages::message (int ev) const
* system
*/

nano::test::system::system () :
nano::test::system::system (nano::test::io_type io_type_a) :
io_type{ io_type_a },
io_ctx{ std::make_shared<boost::asio::io_context> () },
io_guard{ boost::asio::make_work_guard (*io_ctx) }
{
Expand All @@ -40,15 +41,24 @@ nano::test::system::system () :
{
deadline_scaling_factor = std::stod (scale_str);
}

if (io_type == nano::test::io_type::singlethreaded)
{
io_runner = std::make_unique<nano::thread_runner> (io_ctx, /* single threaded */ 1);
}
if (io_type == nano::test::io_type::multithreaded)
{
io_runner = std::make_unique<nano::thread_runner> (io_ctx);
}
}

nano::test::system::system (uint16_t count_a, nano::transport::transport_type type_a, nano::node_flags flags_a) :
system ()
nano::test::system::system (unsigned count_a, nano::test::io_type io_type_a) :
system (io_type_a)
{
nodes.reserve (count_a);
for (uint16_t i (0); i < count_a; ++i)
{
add_node (default_config (), flags_a, type_a);
add_node (default_config ());
}
}

Expand Down Expand Up @@ -85,8 +95,17 @@ void nano::test::system::stop ()
stop_node (*node);
}

io_guard.reset ();
work.stop ();

if (io_guard)
{
io_guard->reset ();
}
if (io_runner)
{
io_runner->stop_event_processing ();
io_runner->join ();
}
}

nano::node & nano::test::system::node (std::size_t index) const
Expand Down Expand Up @@ -221,14 +240,23 @@ void nano::test::system::register_node (std::shared_ptr<nano::node> const & node

void nano::test::system::stop_node (nano::node & node)
{
auto stopped = std::async (std::launch::async, [&node] () {
if (io_type == nano::test::io_type::lazy)
{
// Keep io context running while stopping node
auto stopped = std::async (std::launch::async, [&node] () {
node.stop ();
});
auto ec = poll_until_true (5s, [&] () {
auto status = stopped.wait_for (0s);
return status == std::future_status::ready;
});
debug_assert (!ec);
}
else
{
// Io context is constantly running, so we can just stop the node
node.stop ();
});
auto ec = poll_until_true (5s, [&] () {
auto status = stopped.wait_for (0s);
return status == std::future_status::ready;
});
debug_assert (!ec);
}
}

void nano::test::system::ledger_initialization_set (std::vector<nano::keypair> const & reps, nano::amount const & reserve)
Expand Down Expand Up @@ -337,7 +365,14 @@ std::error_code nano::test::system::poll (std::chrono::nanoseconds const & wait_
{
if constexpr (asio_handler_tracking_threshold () == 0)
{
io_ctx->run_one_for (wait_time);
if (io_type == io_type::lazy)
{
io_ctx->run_one_for (wait_time);
}
else
{
std::this_thread::sleep_for (wait_time);
}
}
else
{
Expand Down
19 changes: 16 additions & 3 deletions nano/test_common/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <nano/lib/errors.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/node/node.hpp>

#include <chrono>
Expand Down Expand Up @@ -30,11 +31,18 @@ constexpr size_t asio_handler_tracking_threshold ()
#endif
}

enum class io_type
{
lazy,
singlethreaded,
multithreaded,
};

class system final
{
public:
system ();
system (uint16_t, nano::transport::transport_type = nano::transport::transport_type::tcp, nano::node_flags = nano::node_flags ());
system (nano::test::io_type = io_type::singlethreaded);
system (unsigned node_count, nano::test::io_type = io_type::singlethreaded);
~system ();

void stop ();
Expand Down Expand Up @@ -88,10 +96,15 @@ class system final
uint16_t get_available_port ();

public:
nano::test::io_type const io_type;

std::shared_ptr<boost::asio::io_context> io_ctx;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> io_guard;
std::optional<nano::io_guard> io_guard;
std::unique_ptr<nano::thread_runner> io_runner; // May be nullptr for lazy I/O

std::vector<std::shared_ptr<nano::node>> nodes;
std::vector<std::shared_ptr<nano::node>> disconnected_nodes;

nano::stats stats;
nano::logger logger{ "tests" };
nano::work_pool work{ nano::dev::network_params.network, std::max (nano::hardware_concurrency (), 1u) };
Expand Down

0 comments on commit 59c3dd2

Please sign in to comment.