From b948af3deb8b7eaa843d366d4b7d2174e0dd46d3 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Fri, 16 Feb 2024 22:29:43 -0500 Subject: [PATCH] Fix test result combiner, fix test endian, enable broadcaster tests. --- test/async/desubscriber.cpp | 76 ++++----- test/async/subscriber.cpp | 24 +-- test/log/tracker.cpp | 4 +- test/net/broadcaster.cpp | 326 ++++++++++++++---------------------- test/net/distributor.cpp | 20 +-- test/net/proxy.cpp | 8 +- 6 files changed, 190 insertions(+), 268 deletions(-) diff --git a/test/async/desubscriber.cpp b/test/async/desubscriber.cpp index ac9eda4c8..d756d7e49 100644 --- a/test/async/desubscriber.cpp +++ b/test/async/desubscriber.cpp @@ -35,24 +35,24 @@ BOOST_AUTO_TEST_CASE(desubscriber__subscribe__stopped__subscriber_stopped) std::pair retry_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= is_zero(instance.size()); - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= is_zero(instance.size()); + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; return true; }, 0); - result |= is_one(instance.size()); + result &= is_one(instance.size()); instance.stop(ec, expected); - result |= is_zero(instance.size()); - result |= (instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= is_zero(instance.size()); + result &= (instance.subscribe([&](code value, size_t size) NOEXCEPT { retry_result = { value, size }; return true; }, 0) == error::subscriber_stopped); - result |= is_zero(instance.size()); + result &= is_zero(instance.size()); }); pool.stop(); @@ -77,13 +77,13 @@ BOOST_AUTO_TEST_CASE(desubscriber__subscribe__exists__subscriber_exists) std::pair second_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { first_result = { value, size }; return true; }, 42); - result |= (instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= (instance.subscribe([&](code value, size_t size) NOEXCEPT { second_result = { value, size }; return true; @@ -116,8 +116,8 @@ BOOST_AUTO_TEST_CASE(desubscriber__subscribe__removed__expected) std::pair second_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= is_zero(instance.size()); - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= is_zero(instance.size()); + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { first_result = { value, size }; return false; @@ -125,15 +125,15 @@ BOOST_AUTO_TEST_CASE(desubscriber__subscribe__removed__expected) instance.notify(ec1, expected1); - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { second_result = { value, size }; return true; }, 42); - result |= is_one(instance.size()); + result &= is_one(instance.size()); instance.stop(ec2, expected2); - result |= is_zero(instance.size()); + result &= is_zero(instance.size()); }); pool.stop(); @@ -158,22 +158,22 @@ BOOST_AUTO_TEST_CASE(desubscriber__subscribe__unique__expected) std::pair second_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= is_zero(instance.size()); - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= is_zero(instance.size()); + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { first_result = { value, size }; return true; }, 42); - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { second_result = { value, size }; return true; }, 99); - result |= (instance.size() == two); + result &= (instance.size() == two); instance.stop(ec, expected); - result |= is_zero(instance.size()); + result &= is_zero(instance.size()); }); pool.stop(); @@ -197,7 +197,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__stop_default__once__expected) std::pair stop_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; return true; @@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__stop__once__expected) std::pair stop_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; return true; @@ -253,7 +253,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__stop__twice__second_dropped) std::pair stop_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; return true; @@ -285,7 +285,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify__stopped__dropped) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Allow first and possible second notify, ignore stop. if (++count != two) notify_result = { value, size }; @@ -317,7 +317,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify__once__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { if (is_one(++count)) notify_result = { value, size }; return true; @@ -349,7 +349,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify__twice_true__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Exclude stop_default call. if (++count <= two) notify_result = { value, size }; @@ -382,7 +382,7 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify__twice_false__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { notify_result = { value, size }; return false; @@ -416,17 +416,17 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify_one__stopped__dropped) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Allow first and possible second notify, ignore stop. if (++count != two) notify_result = { value, size }; return true; }, key); - result |= instance.notify_one(key, ec, expected); + result &= instance.notify_one(key, ec, expected); instance.stop_default(error::address_blocked); - result |= !instance.notify_one(key, error::address_blocked, {}); + result &= !instance.notify_one(key, error::address_blocked, {}); }); pool.stop(); @@ -449,14 +449,14 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify_one__missing__false) std::pair notify_result{}; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Record only first notification. if (is_one(++count)) notify_result = { value, size }; return true; }, 99); - result |= !instance.notify_one(100, error::address_blocked, 21); + result &= !instance.notify_one(100, error::address_blocked, 21); // First notification, and clears map. instance.stop(ec, expected); @@ -483,13 +483,13 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify_one__once__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { if (is_one(++count)) notify_result = { value, size }; return true; }, key); - result |= instance.notify_one(key, ec, expected); + result &= instance.notify_one(key, ec, expected); // Prevents unstopped assertion (uncleared). instance.stop_default(error::address_blocked); @@ -516,15 +516,15 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify_one__twice_true__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Exclude stop_default call. if (++count <= two) notify_result = { value, size }; return true; }, key); - result |= instance.notify_one(key, {}, {}); - result |= instance.notify_one(key, ec, expected); + result &= instance.notify_one(key, {}, {}); + result &= instance.notify_one(key, ec, expected); // Prevents unstopped assertion (uncleared). instance.stop_default(error::address_blocked); @@ -550,14 +550,14 @@ BOOST_AUTO_TEST_CASE(desubscriber__notify_one__twice_false__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { notify_result = { value, size }; return false; }, key); - result |= instance.notify_one(key, ec, expected); - result |= !instance.notify_one(key, {}, {}); + result &= instance.notify_one(key, ec, expected); + result &= !instance.notify_one(key, {}, {}); // Cleared by false return. ////instance.stop_default(error::address_blocked); diff --git a/test/async/subscriber.cpp b/test/async/subscriber.cpp index e1371b29d..db8f35c39 100644 --- a/test/async/subscriber.cpp +++ b/test/async/subscriber.cpp @@ -35,22 +35,22 @@ BOOST_AUTO_TEST_CASE(subscriber__subscribe__stopped__subscriber_stopped) std::pair retry_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= is_zero(instance.size()); - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= is_zero(instance.size()); + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; }); - result |= is_one(instance.size()); + result &= is_one(instance.size()); instance.stop(ec, expected); - result |= is_zero(instance.size()); - result |= (instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= is_zero(instance.size()); + result &= (instance.subscribe([&](code value, size_t size) NOEXCEPT { retry_result = { value, size }; }) == error::subscriber_stopped); - result |= is_zero(instance.size()); + result &= is_zero(instance.size()); }); pool.stop(); @@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE(subscriber__stop_default__once__expected) std::pair stop_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; }); @@ -103,7 +103,7 @@ BOOST_AUTO_TEST_CASE(subscriber__stop__once__expected) std::pair stop_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; }); @@ -131,7 +131,7 @@ BOOST_AUTO_TEST_CASE(subscriber__stop__twice__second_dropped) std::pair stop_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { stop_result = { value, size }; }); @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(subscriber__notify__stopped__dropped) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Allow first and possible second notify, ignore stop. if (++count != two) notify_result = { value, size }; @@ -194,7 +194,7 @@ BOOST_AUTO_TEST_CASE(subscriber__notify__once__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { if (is_one(++count)) notify_result = { value, size }; }); @@ -226,7 +226,7 @@ BOOST_AUTO_TEST_CASE(subscriber__notify__twice__expected) std::pair notify_result; boost::asio::post(strand, [&]() NOEXCEPT { - result |= !instance.subscribe([&](code value, size_t size) NOEXCEPT + result &= !instance.subscribe([&](code value, size_t size) NOEXCEPT { // Exclude stop_default call. if (++count <= two) notify_result = { value, size }; diff --git a/test/log/tracker.cpp b/test/log/tracker.cpp index 7eb8fbf82..e25911e87 100644 --- a/test/log/tracker.cpp +++ b/test/log/tracker.cpp @@ -52,13 +52,13 @@ BOOST_AUTO_TEST_CASE(tracker__construct__guarded__safe_expected_messages) if (is_zero(count++)) { const auto expected = std::string{ typeid(tracked).name() } + "(1)\n"; - result |= (message == expected); + result &= (message == expected); return true; } else { const auto expected = std::string{ typeid(tracked).name() } + "(0)~\n"; - result |= (message == expected); + result &= (message == expected); log_stopped.set_value(ec); return false; } diff --git a/test/net/broadcaster.cpp b/test/net/broadcaster.cpp index 30319a144..843be70ae 100644 --- a/test/net/broadcaster.cpp +++ b/test/net/broadcaster.cpp @@ -1,202 +1,124 @@ -/////** -//// * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) -//// * -//// * This file is part of libbitcoin. -//// * -//// * This program is free software: you can redistribute it and/or modify -//// * it under the terms of the GNU Affero General Public License as published by -//// * the Free Software Foundation, either version 3 of the License, or -//// * (at your option) any later version. -//// * -//// * This program is distributed in the hope that it will be useful, -//// * but WITHOUT ANY WARRANTY; without even the implied warranty of -//// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -//// * GNU Affero General Public License for more details. -//// * -//// * You should have received a copy of the GNU Affero General Public License -//// * along with this program. If not, see . -//// */ -////#include "../test.hpp" -//// -////BOOST_AUTO_TEST_SUITE(broadcaster_tests) -//// -////BOOST_AUTO_TEST_CASE(broadcaster__construct__stop__stops) -////{ -//// threadpool pool(2); -//// asio::strand strand(pool.service().get_executor()); -//// broadcaster instance(strand); -//// -//// std::promise promise; -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.stop(error::service_stopped); -//// promise.set_value(true); -//// }); -//// -//// pool.stop(); -//// BOOST_REQUIRE(pool.join()); -//// BOOST_REQUIRE(promise.get_future().get()); -////} -//// -////BOOST_AUTO_TEST_CASE(broadcaster__subscribe__stop__expected_code) -////{ -//// threadpool pool(2); -//// asio::strand strand(pool.service().get_executor()); -//// broadcaster instance(strand); -//// constexpr auto expected_ec = error::invalid_magic; -//// auto result = true; -//// -//// std::promise promise; -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT -//// { -//// // Stop notification has nullptr message and specified code. -//// result |= is_null(ping); -//// promise.set_value(ec); -//// return true; -//// }); -//// }); -//// -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.stop(expected_ec); -//// }); -//// -//// pool.stop(); -//// BOOST_REQUIRE(pool.join()); -//// BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec); -//// BOOST_REQUIRE(result); -////} -//// -////BOOST_AUTO_TEST_CASE(broadcaster__notify__invalid_message__no_notification) -////{ -//// threadpool pool(2); -//// asio::strand strand(pool.service().get_executor()); -//// broadcaster instance(strand); -//// constexpr auto expected_ec = error::invalid_magic; -//// auto result = true; -//// -//// // Subscription will capture only the stop notification. -//// std::promise promise; -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT -//// { -//// result |= is_null(ping); -//// promise.set_value(ec); -//// return true; -//// }); -//// }); -//// -//// // Invalid object deserialization will not cause a notification. -//// system::data_chunk empty{}; -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// constexpr auto nonced_ping_version = messages::level::bip31; -//// -//// // This line throws and is caught internal to the low level stream. -//// const auto ec = instance.notify(messages::identifier::ping, nonced_ping_version, empty); -//// result |= (ec == error::invalid_message); -//// }); -//// -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.stop(expected_ec); -//// }); -//// -//// pool.stop(); -//// BOOST_REQUIRE(pool.join()); -//// BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec); -//// BOOST_REQUIRE(result); -////} -//// -////BOOST_AUTO_TEST_CASE(broadcaster__notify__valid_message_invalid_version__no_notification) -////{ -//// threadpool pool(2); -//// asio::strand strand(pool.service().get_executor()); -//// broadcaster instance(strand); -//// constexpr auto expected_ec = error::invalid_magic; -//// auto result = true; -//// -//// // Subscription will capture only the stop notification. -//// std::promise promise; -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT -//// { -//// result |= is_null(ping); -//// promise.set_value(ec); -//// return true; -//// }); -//// }); -//// -//// // Invalid object version will not cause a notification. -//// const auto ping = system::to_little_endian_size(42, sizeof(uint64_t)); -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// constexpr uint32_t invalid_ping_version = 0; -//// const auto ec = instance.notify(messages::identifier::ping, invalid_ping_version, ping); -//// result |= (ec == error::invalid_message); -//// }); -//// -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.stop(expected_ec); -//// }); -//// -//// pool.stop(); -//// BOOST_REQUIRE(pool.join()); -//// BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec); -//// BOOST_REQUIRE(result); -////} -//// -////BOOST_AUTO_TEST_CASE(broadcaster__notify__valid_nonced_ping__expected_notification) -////{ -//// threadpool pool(2); -//// asio::strand strand(pool.service().get_executor()); -//// broadcaster instance(strand); -//// constexpr uint64_t expected_nonce = 42; -//// constexpr auto expected_ec = error::invalid_magic; -//// auto result = true; -//// -//// // Subscription will capture message and stop notifications. -//// std::promise promise; -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT -//// { -//// // Handle stop notification (unavoidable test condition). -//// if (!ping) -//// { -//// promise.set_value(ec); -//// return true; -//// } -//// -//// // Handle message notification. -//// result |= (ping->nonce == expected_nonce); -//// result |= (ec == error::success); -//// return true; -//// }); -//// }); -//// -//// const auto ping = system::to_little_endian_size(expected_nonce, sizeof(uint64_t)); -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// constexpr auto nonced_ping_version = messages::level::bip31; -//// const auto ec = instance.notify(messages::identifier::ping, nonced_ping_version, ping); -//// result |= (ec == error::success); -//// }); -//// -//// boost::asio::post(strand, [&]() NOEXCEPT -//// { -//// instance.stop(expected_ec); -//// }); -//// -//// pool.stop(); -//// BOOST_REQUIRE(pool.join()); -//// BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec); -//// BOOST_REQUIRE(result); -////} -//// -////BOOST_AUTO_TEST_SUITE_END() +/** + * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "../test.hpp" + +BOOST_AUTO_TEST_SUITE(broadcaster_tests) + +BOOST_AUTO_TEST_CASE(broadcaster__construct__stop__stops) +{ + threadpool pool(2); + asio::strand strand(pool.service().get_executor()); + broadcaster instance(strand); + + std::promise promise; + boost::asio::post(strand, [&]() NOEXCEPT + { + instance.stop(error::service_stopped); + promise.set_value(true); + }); + + pool.stop(); + BOOST_REQUIRE(pool.join()); + BOOST_REQUIRE(promise.get_future().get()); +} + +BOOST_AUTO_TEST_CASE(broadcaster__subscribe__stop__expected_code) +{ + threadpool pool(2); + asio::strand strand(pool.service().get_executor()); + broadcaster instance(strand); + constexpr uint64_t channel_id = 42; + constexpr auto expected_ec = error::invalid_magic; + auto result = true; + + std::promise promise; + boost::asio::post(strand, [&]() NOEXCEPT + { + BOOST_REQUIRE(!instance.subscribe( + [&](const code& ec, const messages::ping::cptr& ping, broadcaster::channel_id id) NOEXCEPT + { + // Stop notification has nullptr message, zero id, and specified code. + result &= is_null(ping); + result &= is_zero(id); + promise.set_value(ec); + return true; + }, channel_id)); + }); + + boost::asio::post(strand, [&]() NOEXCEPT + { + instance.stop(expected_ec); + }); + + pool.stop(); + BOOST_REQUIRE(pool.join()); + BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec); + BOOST_REQUIRE(result); +} + +BOOST_AUTO_TEST_CASE(broadcaster__notify__valid_nonced_ping__expected_notification) +{ + threadpool pool(2); + asio::strand strand(pool.service().get_executor()); + broadcaster instance(strand); + constexpr uint64_t channel_id = 42; + constexpr uint64_t expected_nonce = 42; + constexpr auto expected_ec = error::invalid_magic; + auto result = true; + + // Subscription will capture message and stop notifications. + std::promise promise; + boost::asio::post(strand, [&]() NOEXCEPT + { + BOOST_REQUIRE(!instance.subscribe( + [&](const code& ec, const messages::ping::cptr& ping, broadcaster::channel_id id) NOEXCEPT + { + // Handle stop notification (unavoidable test condition). + if (!ping) + { + promise.set_value(ec); + return true; + } + + // Handle message notification. + result &= (ping->nonce == expected_nonce); + result &= (ec == error::success); + result &= (id == channel_id); + return true; + }, channel_id)); + }); + + const auto ping = std::make_shared(expected_nonce); + boost::asio::post(strand, [&]() NOEXCEPT + { + instance.notify(ping, channel_id); + }); + + boost::asio::post(strand, [&]() NOEXCEPT + { + instance.stop(expected_ec); + }); + + pool.stop(); + BOOST_REQUIRE(pool.join()); + BOOST_REQUIRE_EQUAL(promise.get_future().get(), expected_ec); + BOOST_REQUIRE(result); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/net/distributor.cpp b/test/net/distributor.cpp index 8a3c40193..bcfae4d6a 100644 --- a/test/net/distributor.cpp +++ b/test/net/distributor.cpp @@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(distributor__subscribe__stop__expected_code) instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT { // Stop notification has nullptr message and specified code. - result |= is_null(ping); + result &= is_null(ping); promise.set_value(ec); return true; }); @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(distributor__notify__invalid_message__no_notification) { instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT { - result |= is_null(ping); + result &= is_null(ping); promise.set_value(ec); return true; }); @@ -97,7 +97,7 @@ BOOST_AUTO_TEST_CASE(distributor__notify__invalid_message__no_notification) // This line throws and is caught internal to the low level stream. const auto ec = instance.notify(messages::identifier::ping, nonced_ping_version, empty); - result |= (ec == error::invalid_message); + result &= (ec == error::invalid_message); }); boost::asio::post(strand, [&]() NOEXCEPT @@ -125,19 +125,19 @@ BOOST_AUTO_TEST_CASE(distributor__notify__valid_message_invalid_version__no_noti { instance.subscribe([&](const code& ec, const messages::ping::cptr& ping) NOEXCEPT { - result |= is_null(ping); + result &= is_null(ping); promise.set_value(ec); return true; }); }); // Invalid object version will not cause a notification. - const auto ping = system::to_little_endian_size(42, sizeof(uint64_t)); + const auto ping = system::to_chunk(system::to_little_endian(42)); boost::asio::post(strand, [&]() NOEXCEPT { constexpr uint32_t invalid_ping_version = 0; const auto ec = instance.notify(messages::identifier::ping, invalid_ping_version, ping); - result |= (ec == error::invalid_message); + result &= (ec == error::invalid_message); }); boost::asio::post(strand, [&]() NOEXCEPT @@ -174,18 +174,18 @@ BOOST_AUTO_TEST_CASE(distributor__notify__valid_nonced_ping__expected_notificati } // Handle message notification. - result |= (ping->nonce == expected_nonce); - result |= (ec == error::success); + result &= (ping->nonce == expected_nonce); + result &= (ec == error::success); return true; }); }); - const auto ping = system::to_little_endian_size(expected_nonce, sizeof(uint64_t)); + const auto ping = system::to_chunk(system::to_little_endian(expected_nonce)); boost::asio::post(strand, [&]() NOEXCEPT { constexpr auto nonced_ping_version = messages::level::bip31; const auto ec = instance.notify(messages::identifier::ping, nonced_ping_version, ping); - result |= (ec == error::success); + result &= (ec == error::success); }); boost::asio::post(strand, [&]() NOEXCEPT diff --git a/test/net/proxy.cpp b/test/net/proxy.cpp index 226417325..3e948de95 100644 --- a/test/net/proxy.cpp +++ b/test/net/proxy.cpp @@ -322,7 +322,7 @@ BOOST_AUTO_TEST_CASE(proxy__subscribe_message__subscribed__expected) proxy_ptr->subscribe_message( [&](code ec, messages::ping::cptr ping) NOEXCEPT { - result |= is_null(ping); + result &= is_null(ping); message_stopped.set_value(ec); return true; }); @@ -369,7 +369,7 @@ BOOST_AUTO_TEST_CASE(proxy__stop__all_subscribed__expected) proxy_ptr->subscribe_message( [&](code ec, messages::ping::cptr ping) NOEXCEPT { - result |= is_null(ping); + result &= is_null(ping); message_stopped.set_value(ec); return true; }); @@ -398,7 +398,7 @@ BOOST_AUTO_TEST_CASE(proxy__send__not_connected__expected) const auto handler = [&](code ec) NOEXCEPT { // Send failure causes stop before handler invoked. - result |= proxy_ptr->stopped(); + result &= proxy_ptr->stopped(); promise.set_value(ec); }; @@ -426,7 +426,7 @@ BOOST_AUTO_TEST_CASE(proxy__send__not_connected_move__expected) proxy_ptr->send(messages::ping{ 42 }, [&](code ec) { // Send failure causes stop before handler invoked. - result |= proxy_ptr->stopped(); + result &= proxy_ptr->stopped(); promise.set_value(ec); }); });