mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
introduce and use the generalized node::Warnings interface
Instead of having separate warning functions (and globals) for each different warning that can be raised, encapsulate this logic into a single class and allow to (un)set any number of warnings. Introduces behaviour change: - the `-alertnotify` command is executed for all `KernelNotifications::warningSet` calls, which now also covers the `LARGE_WORK_INVALID_CHAIN` warning. - previously, warnings were returned based on a predetermined order, e.g. with the "pre-release test build" warning always first. This is no longer the case, and Warnings::GetMessages() will return messages sorted by the id of the warning. Removes warnings.cpp from kernel.
This commit is contained in:
52
src/test/node_warnings_tests.cpp
Normal file
52
src/test/node_warnings_tests.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2024-present The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
//
|
||||
|
||||
#include <node/warnings.h>
|
||||
#include <util/translation.h>
|
||||
|
||||
#include <test/util/setup_common.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(node_warnings_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(warnings)
|
||||
{
|
||||
node::Warnings warnings;
|
||||
// On pre-release builds, a warning is generated automatically
|
||||
warnings.Unset(node::Warning::PRE_RELEASE_TEST_BUILD);
|
||||
|
||||
// For these tests, we don't care what the exact warnings are, so
|
||||
// just refer to them as warning_1 and warning_2
|
||||
const auto warning_1{node::Warning::CLOCK_OUT_OF_SYNC};
|
||||
const auto warning_2{node::Warning::FATAL_INTERNAL_ERROR};
|
||||
|
||||
// Ensure we start without any warnings
|
||||
BOOST_CHECK(warnings.GetMessages().size() == 0);
|
||||
// Add two warnings
|
||||
BOOST_CHECK(warnings.Set(warning_1, _("warning 1")));
|
||||
BOOST_CHECK(warnings.Set(warning_2, _("warning 2")));
|
||||
// Unset the second one
|
||||
BOOST_CHECK(warnings.Unset(warning_2));
|
||||
// Since it's already been unset, this should return false
|
||||
BOOST_CHECK(!warnings.Unset(warning_2));
|
||||
// We should now be able to set w2 again
|
||||
BOOST_CHECK(warnings.Set(warning_2, _("warning 2 - revision 1")));
|
||||
// Setting w2 again should return false since it's already set
|
||||
BOOST_CHECK(!warnings.Set(warning_2, _("warning 2 - revision 2")));
|
||||
|
||||
// Verify messages are correct
|
||||
const auto messages{warnings.GetMessages()};
|
||||
BOOST_CHECK(messages.size() == 2);
|
||||
BOOST_CHECK(messages[0].original == "warning 1");
|
||||
BOOST_CHECK(messages[1].original == "warning 2 - revision 1");
|
||||
|
||||
// Clearing all warnings should also clear all messages
|
||||
BOOST_CHECK(warnings.Unset(warning_1));
|
||||
BOOST_CHECK(warnings.Unset(warning_2));
|
||||
BOOST_CHECK(warnings.GetMessages().size() == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
Reference in New Issue
Block a user