Fix tests

This commit is contained in:
Piotr Wójcik 2025-05-22 19:11:56 +02:00
commit e104e5995b
2 changed files with 65 additions and 81 deletions

View file

@ -114,10 +114,11 @@ TEST (socket, disconnection_of_silent_connections)
// Just to ensure the disconnection wasn't due to the timer timeout. // Just to ensure the disconnection wasn't due to the timer timeout.
ASSERT_EQ (0, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::in)); ASSERT_EQ (0, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::in));
// Asserts the silent checker worked. // Asserts the silent checker worked.
ASSERT_EQ (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_silent_connection_drop, nano::stat::dir::in)); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_silent_connection_drop, nano::stat::dir::in));
} }
TEST (socket, drop_policy) // FIXME: Socket no longer queues writes, so this test is no longer valid
TEST (socket, DISABLED_drop_policy)
{ {
nano::test::system system; nano::test::system system;
@ -180,9 +181,8 @@ TEST (socket, drop_policy)
ASSERT_EQ (0, failed_writes); ASSERT_EQ (0, failed_writes);
} }
// This is abusing the socket class, it's interfering with the normal node lifetimes and as a result deadlocks // FIXME: Socket no longer queues writes, so this test is no longer valid
// TEST (socket, DISABLED_concurrent_writes) TEST (socket, DISABLED_concurrent_writes)
TEST (socket, concurrent_writes)
{ {
nano::test::system system; nano::test::system system;
@ -369,16 +369,14 @@ TEST (socket_timeout, read)
// check that the callback was called and we got an error // check that the callback was called and we got an error
ASSERT_TIMELY_EQ (10s, done, true); ASSERT_TIMELY_EQ (10s, done, true);
ASSERT_TRUE (ec); ASSERT_TRUE (ec);
ASSERT_EQ (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_read_error, nano::stat::dir::in)); ASSERT_EQ (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_read_error));
// check that the socket was closed due to tcp_io_timeout timeout // check that the socket was closed due to tcp_io_timeout timeout
ASSERT_EQ (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::out)); ASSERT_EQ (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::in));
} }
TEST (socket_timeout, write) TEST (socket_timeout, write)
{ {
std::atomic<bool> done = false;
// create one node and set timeout to 1 second // create one node and set timeout to 1 second
nano::test::system system (1); nano::test::system system (1);
std::shared_ptr<nano::node> node = system.nodes[0]; std::shared_ptr<nano::node> node = system.nodes[0];
@ -386,13 +384,13 @@ TEST (socket_timeout, write)
// create a server socket // create a server socket
boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::address_v6::loopback (), system.get_available_port ()); boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::address_v6::loopback (), system.get_available_port ());
boost::asio::ip::tcp::acceptor acceptor (*system.io_ctx); boost::asio::ip::tcp::acceptor acceptor (node->io_ctx);
acceptor.open (endpoint.protocol ()); acceptor.open (endpoint.protocol ());
acceptor.bind (endpoint); acceptor.bind (endpoint);
acceptor.listen (boost::asio::socket_base::max_listen_connections); acceptor.listen (boost::asio::socket_base::max_listen_connections);
// asynchronously accept an incoming connection and create a newsock and do not receive any data // asynchronously accept an incoming connection and create a newsock and do not receive any data
boost::asio::ip::tcp::socket newsock (*system.io_ctx); boost::asio::ip::tcp::socket newsock (node->io_ctx);
acceptor.async_accept (newsock, [] (boost::system::error_code const & ec_a) { acceptor.async_accept (newsock, [] (boost::system::error_code const & ec_a) {
EXPECT_FALSE (ec_a); EXPECT_FALSE (ec_a);
}); });
@ -402,24 +400,26 @@ TEST (socket_timeout, write)
// and the timeout should kick in and close the socket, which will cause the async_write to return an error // and the timeout should kick in and close the socket, which will cause the async_write to return an error
auto socket = std::make_shared<nano::transport::tcp_socket> (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers auto socket = std::make_shared<nano::transport::tcp_socket> (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers
socket->async_connect (acceptor.local_endpoint (), [&socket, &done] (boost::system::error_code const & ec_a) { auto ec_0 = socket->blocking_connect (acceptor.local_endpoint ());
EXPECT_FALSE (ec_a); ASSERT_FALSE (ec_0);
auto buffer = std::make_shared<std::vector<uint8_t>> (128 * 1024); auto buffer = std::make_shared<std::vector<uint8_t>> (128 * 1024);
// At some point the write should fail with a timeout
boost::system::error_code ec;
for (auto i = 0; i < 1024; ++i) for (auto i = 0; i < 1024; ++i)
{ {
socket->async_write (nano::shared_const_buffer{ buffer }, [&done] (boost::system::error_code const & ec_a, size_t size_a) { auto [ec_1, size_1] = socket->blocking_write (buffer, buffer->size ());
if (ec_a) if (ec_1)
{ {
done = true; ec = ec_1;
break;
} }
});
} }
});
// check that the callback was called and we got an error // check that the callback was called and we got an error
ASSERT_TIMELY (10s, done); ASSERT_TRUE (ec);
ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_write_error, nano::stat::dir::in)); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_write_error));
// check that the socket was closed due to tcp_io_timeout timeout // check that the socket was closed due to tcp_io_timeout timeout
ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::out)); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::out));
@ -434,60 +434,48 @@ TEST (socket_timeout, read_overlapped)
// create a server socket // create a server socket
boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::address_v6::loopback (), system.get_available_port ()); boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::address_v6::loopback (), system.get_available_port ());
boost::asio::ip::tcp::acceptor acceptor (*system.io_ctx); boost::asio::ip::tcp::acceptor acceptor (node->io_ctx);
acceptor.open (endpoint.protocol ()); acceptor.open (endpoint.protocol ());
acceptor.bind (endpoint); acceptor.bind (endpoint);
acceptor.listen (boost::asio::socket_base::max_listen_connections); acceptor.listen (boost::asio::socket_base::max_listen_connections);
// asynchronously accept an incoming connection and send one byte only // asynchronously accept an incoming connection and send one byte only
boost::asio::ip::tcp::socket newsock (*system.io_ctx); boost::asio::ip::tcp::socket newsock (node->io_ctx);
acceptor.async_accept (newsock, [&newsock] (boost::system::error_code const & ec_a) { acceptor.async_accept (newsock, [&newsock] (boost::system::error_code const & ec_a) {
EXPECT_FALSE (ec_a); EXPECT_FALSE (ec_a);
auto buffer = std::make_shared<std::vector<uint8_t>> (1); auto buffer = std::make_shared<std::vector<uint8_t>> (1);
nano::async_write (newsock, nano::shared_const_buffer (buffer), [] (boost::system::error_code const & ec_a, size_t size_a) { nano::async_write (newsock, nano::shared_const_buffer (buffer), [] (boost::system::error_code const & ec_a, size_t size_a) {
EXPECT_TRUE (!ec_a); EXPECT_TRUE (!ec_a);
EXPECT_TRUE (size_a == 1); EXPECT_EQ (size_a, 1);
}); });
}); });
// create a client socket to connect and call async_read twice, the second call should time out // create a client socket to connect and call async_read twice, the second call should time out
auto socket = std::make_shared<nano::transport::tcp_socket> (*node); auto socket = std::make_shared<nano::transport::tcp_socket> (*node);
std::atomic<bool> done = false;
boost::system::error_code ec; auto ec_0 = socket->blocking_connect (acceptor.local_endpoint ());
socket->async_connect (acceptor.local_endpoint (), [&socket, &ec, &done] (boost::system::error_code const & ec_a) { ASSERT_FALSE (ec_0);
EXPECT_FALSE (ec_a);
auto buffer = std::make_shared<std::vector<uint8_t>> (1); auto buffer = std::make_shared<std::vector<uint8_t>> (1);
socket->async_read (buffer, 1, [] (boost::system::error_code const & ec_a, size_t size_a) { auto [ec_1, size_1] = socket->blocking_read (buffer, 1);
EXPECT_FALSE (ec_a); ASSERT_FALSE (ec_1);
EXPECT_TRUE (size_a == 1); ASSERT_TRUE (size_1 == 1);
});
socket->async_read (buffer, 1, [&ec, &done] (boost::system::error_code const & ec_a, size_t size_a) { auto [ec_2, size_2] = socket->blocking_read (buffer, 1);
EXPECT_EQ (size_a, 0); ASSERT_EQ (size_2, 0);
if (ec_a) ASSERT_TRUE (ec_2);
{
ec = ec_a;
done = true;
}
});
});
// check that the callback was called and we got an error // check that the callback was called and we got an error
ASSERT_TIMELY_EQ (10s, done, true); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_read_error));
ASSERT_TRUE (ec);
ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_read_error, nano::stat::dir::in));
// check that the socket was closed due to tcp_io_timeout timeout // check that the socket was closed due to tcp_io_timeout timeout
ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::out)); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::in));
} }
TEST (socket_timeout, write_overlapped) TEST (socket_timeout, write_overlapped)
{ {
std::atomic<bool> done = false;
// create one node and set timeout to 1 second // create one node and set timeout to 1 second
nano::test::system system (1); nano::test::system system (1);
std::shared_ptr<nano::node> node = system.nodes[0]; std::shared_ptr<nano::node> node = system.nodes[0];
@ -495,13 +483,13 @@ TEST (socket_timeout, write_overlapped)
// create a server socket // create a server socket
boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::address_v6::loopback (), system.get_available_port ()); boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::address_v6::loopback (), system.get_available_port ());
boost::asio::ip::tcp::acceptor acceptor (*system.io_ctx); boost::asio::ip::tcp::acceptor acceptor (node->io_ctx);
acceptor.open (endpoint.protocol ()); acceptor.open (endpoint.protocol ());
acceptor.bind (endpoint); acceptor.bind (endpoint);
acceptor.listen (boost::asio::socket_base::max_listen_connections); acceptor.listen (boost::asio::socket_base::max_listen_connections);
// asynchronously accept an incoming connection and read 2 bytes only // asynchronously accept an incoming connection and read 2 bytes only
boost::asio::ip::tcp::socket newsock (*system.io_ctx); boost::asio::ip::tcp::socket newsock (node->io_ctx);
auto buffer = std::make_shared<std::vector<uint8_t>> (1); auto buffer = std::make_shared<std::vector<uint8_t>> (1);
acceptor.async_accept (newsock, [&newsock, &buffer] (boost::system::error_code const & ec_a) { acceptor.async_accept (newsock, [&newsock, &buffer] (boost::system::error_code const & ec_a) {
EXPECT_FALSE (ec_a); EXPECT_FALSE (ec_a);
@ -515,30 +503,33 @@ TEST (socket_timeout, write_overlapped)
// create a client socket and send lots of data to fill the socket queue on the local and remote side // create a client socket and send lots of data to fill the socket queue on the local and remote side
// eventually, the all tcp queues should fill up and async_write will not be able to progress // eventually, the all tcp queues should fill up and async_write will not be able to progress
// and the timeout should kick in and close the socket, which will cause the async_write to return an error // and the timeout should kick in and close the socket, which will cause the async_write to return an error
auto socket = std::make_shared<nano::transport::tcp_socket> (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers auto socket = std::make_shared<nano::transport::tcp_socket> (*node, nano::transport::socket_endpoint::client);
socket->async_connect (acceptor.local_endpoint (), [&socket, &done] (boost::system::error_code const & ec_a) {
EXPECT_FALSE (ec_a); auto ec_0 = socket->blocking_connect (acceptor.local_endpoint ());
ASSERT_FALSE (ec_0);
auto buffer1 = std::make_shared<std::vector<uint8_t>> (1); auto buffer1 = std::make_shared<std::vector<uint8_t>> (1);
auto buffer2 = std::make_shared<std::vector<uint8_t>> (128 * 1024); auto buffer2 = std::make_shared<std::vector<uint8_t>> (128 * 1024);
socket->async_write (nano::shared_const_buffer{ buffer1 }, [] (boost::system::error_code const & ec_a, size_t size_a) {
EXPECT_FALSE (ec_a); auto [ec_1, size_1] = socket->blocking_write (nano::shared_const_buffer{ buffer1 }, 1);
EXPECT_EQ (size_a, 1); ASSERT_FALSE (ec_1);
}); ASSERT_EQ (size_1, 1);
for (auto i = 0; i < 1024; ++i)
// At some point the write should fail with a timeout
boost::system::error_code ec;
for (auto i = 0; i < 1000; ++i)
{ {
socket->async_write (nano::shared_const_buffer{ buffer2 }, [&done] (boost::system::error_code const & ec_a, size_t size_a) { auto [ec_2, size_2] = socket->blocking_write (nano::shared_const_buffer{ buffer2 }, 128 * 1024);
if (ec_a) if (ec_2)
{ {
done = true; ec = ec_2;
break;
} }
});
} }
});
// check that the callback was called and we got an error // check that the callback was called and we got an error
ASSERT_TIMELY_EQ (10s, done, true); ASSERT_TRUE (ec);
ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_write_error, nano::stat::dir::in)); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_write_error));
// check that the socket was closed due to tcp_io_timeout timeout // check that the socket was closed due to tcp_io_timeout timeout
ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::out)); ASSERT_LE (1, node->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, nano::stat::dir::out));

View file

@ -159,15 +159,8 @@ TEST (telemetry, dos_tcp)
ASSERT_TIMELY (5s, (nano::dev::network_params.network.telemetry_request_cooldown + orig) <= std::chrono::steady_clock::now ()); ASSERT_TIMELY (5s, (nano::dev::network_params.network.telemetry_request_cooldown + orig) <= std::chrono::steady_clock::now ());
// Should process no more telemetry_req messages // But not respond to all of them (by default there are 2 broadcasts per second in dev mode)
ASSERT_EQ (1, node_server->stats.count (nano::stat::type::message, nano::stat::detail::telemetry_req, nano::stat::dir::in)); ASSERT_ALWAYS (1s, node_server->stats.count (nano::stat::type::message, nano::stat::detail::telemetry_ack, nano::stat::dir::out) < 7);
// Now spam messages waiting for it to be processed
while (node_server->stats.count (nano::stat::type::message, nano::stat::detail::telemetry_req, nano::stat::dir::in) == 1)
{
channel->send (message, nano::transport::traffic_type::test);
ASSERT_NO_ERROR (system.poll ());
}
} }
TEST (telemetry, disable_metrics) TEST (telemetry, disable_metrics)