From 5643d6873102e4be2ceb726254316d60972e31fe Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 6 May 2024 11:49:00 -0400 Subject: [PATCH 1/3] Test cases for error::service_suspended. --- test/net/acceptor.cpp | 33 +++++++++++++++++++++++++++++++++ test/net/connector.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/test/net/acceptor.cpp b/test/net/acceptor.cpp index 719d217cb..88966c9c1 100644 --- a/test/net/acceptor.cpp +++ b/test/net/acceptor.cpp @@ -93,6 +93,39 @@ BOOST_AUTO_TEST_CASE(acceptor__start__stop__success) BOOST_REQUIRE(instance->get_stopped()); } +BOOST_AUTO_TEST_CASE(acceptor__accept__stop_suspended__service_suspended) +{ + // TODO: There is no way to fake successful acceptance. + const logger log{}; + threadpool pool(2); + std::atomic_bool suspended{ true }; + asio::strand strand(pool.service().get_executor()); + settings set(bc::system::chain::selection::mainnet); + auto instance = std::make_shared(log, strand, pool.service(), set, suspended); + + // Result codes inconsistent due to context. + instance->start(42); + + std::pair result{}; + boost::asio::post(strand, [&, instance]() NOEXCEPT + { + instance->accept([&](const code& ec, const socket::ptr& socket) NOEXCEPT + { + result.first = ec; + result.second = socket; + }); + + std::this_thread::sleep_for(microseconds(1)); + instance->stop(); + }); + + pool.stop(); + BOOST_REQUIRE(pool.join()); + BOOST_REQUIRE(instance->get_stopped()); + BOOST_REQUIRE(result.first == error::service_suspended); + BOOST_REQUIRE(!result.second); +} + BOOST_AUTO_TEST_CASE(acceptor__accept__stop__channel_stopped) { // TODO: There is no way to fake successful acceptance. diff --git a/test/net/connector.cpp b/test/net/connector.cpp index 8487453c1..9f080fe1f 100644 --- a/test/net/connector.cpp +++ b/test/net/connector.cpp @@ -80,6 +80,36 @@ class tiny_timeout } }; +BOOST_AUTO_TEST_CASE(connector__connect_address__bogus_address_suspended__service_suspended) +{ + logger log{}; + log.stop(); + threadpool pool(2); + std::atomic_bool suspended{ true }; + asio::strand strand(pool.service().get_executor()); + const tiny_timeout set(bc::system::chain::selection::mainnet); + auto instance = std::make_shared(log, strand, pool.service(), set, suspended); + auto result = true; + + boost::asio::post(strand, [&]() NOEXCEPT + { + // DNS resolve failure (race), timeout includes a socket. + instance->connect(config::address{ config::endpoint{ "42.42.42.42:42" }.to_address() }, + [&](const code& ec, const socket::ptr& socket) NOEXCEPT + { + result &= (ec == error::service_suspended); + result &= is_null(socket); + }); + + std::this_thread::sleep_for(microseconds(1)); + }); + + pool.stop(); + BOOST_REQUIRE(pool.join()); + BOOST_REQUIRE(instance->get_stopped()); + BOOST_REQUIRE(result); +} + BOOST_AUTO_TEST_CASE(connector__connect_address__bogus_address__operation_timeout) { logger log{}; From b3c880affddd6c004f6b8e33320717018a6e4875 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 6 May 2024 17:24:03 -0400 Subject: [PATCH 2/3] Test style. --- test/net/acceptor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/net/acceptor.cpp b/test/net/acceptor.cpp index 88966c9c1..0ea97c9e7 100644 --- a/test/net/acceptor.cpp +++ b/test/net/acceptor.cpp @@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE(acceptor__accept__stop_suspended__service_suspended) pool.stop(); BOOST_REQUIRE(pool.join()); BOOST_REQUIRE(instance->get_stopped()); - BOOST_REQUIRE(result.first == error::service_suspended); + BOOST_REQUIRE_EQUAL(result.first, error::service_suspended); BOOST_REQUIRE(!result.second); } From 3ca76f930e7dfb63f937fee18f1a2940218b592a Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 6 May 2024 19:28:23 -0400 Subject: [PATCH 3/3] Fix test race. --- test/net/acceptor.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/net/acceptor.cpp b/test/net/acceptor.cpp index 0ea97c9e7..c653bb8f3 100644 --- a/test/net/acceptor.cpp +++ b/test/net/acceptor.cpp @@ -93,7 +93,8 @@ BOOST_AUTO_TEST_CASE(acceptor__start__stop__success) BOOST_REQUIRE(instance->get_stopped()); } -BOOST_AUTO_TEST_CASE(acceptor__accept__stop_suspended__service_suspended) +// race +BOOST_AUTO_TEST_CASE(acceptor__accept__stop_suspended__service_stopped_or_suspended) { // TODO: There is no way to fake successful acceptance. const logger log{}; @@ -122,7 +123,7 @@ BOOST_AUTO_TEST_CASE(acceptor__accept__stop_suspended__service_suspended) pool.stop(); BOOST_REQUIRE(pool.join()); BOOST_REQUIRE(instance->get_stopped()); - BOOST_REQUIRE_EQUAL(result.first, error::service_suspended); + BOOST_REQUIRE(result.first == error::service_suspended || result.first == error::service_stopped); BOOST_REQUIRE(!result.second); }