test: Add unit test coverage for Init and Shutdown code

Currently this code is not called in unit tests. Calling should make it
possible to write tests for things like IPC exceptions being thrown during
shutdown.
This commit is contained in:
Ryan Ofsky
2025-04-18 18:12:46 -04:00
parent 9a9fb19536
commit 6eb09fd614
10 changed files with 107 additions and 15 deletions

View File

@@ -64,6 +64,7 @@ add_executable(test_bitcoin
net_peer_eviction_tests.cpp
net_tests.cpp
netbase_tests.cpp
node_init_tests.cpp
node_warnings_tests.cpp
orphanage_tests.cpp
pcp_tests.cpp

View File

@@ -0,0 +1,51 @@
// Copyright (c) 2025 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 <init.h>
#include <interfaces/init.h>
#include <rpc/server.h>
#include <boost/test/unit_test.hpp>
#include <test/util/setup_common.h>
using node::NodeContext;
BOOST_FIXTURE_TEST_SUITE(node_init_tests, BasicTestingSetup)
//! Custom implementation of interfaces::Init for testing.
class TestInit : public interfaces::Init
{
public:
TestInit(NodeContext& node) : m_node(node)
{
InitContext(m_node);
m_node.init = this;
}
std::unique_ptr<interfaces::Chain> makeChain() override { return interfaces::MakeChain(m_node); }
std::unique_ptr<interfaces::WalletLoader> makeWalletLoader(interfaces::Chain& chain) override
{
return MakeWalletLoader(chain, *Assert(m_node.args));
}
NodeContext& m_node;
};
BOOST_AUTO_TEST_CASE(init_test)
{
// Clear state set by BasicTestingSetup that AppInitMain assumes is unset.
LogInstance().DisconnectTestLogger();
m_node.args->SetConfigFilePath({});
// Prevent the test from trying to listen on ports 8332 and 8333.
m_node.args->ForceSetArg("-server", "0");
m_node.args->ForceSetArg("-listen", "0");
// Run through initialization and shutdown code.
TestInit init{m_node};
BOOST_CHECK(AppInitInterfaces(m_node));
BOOST_CHECK(AppInitMain(m_node));
Interrupt(m_node);
Shutdown(m_node);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@@ -115,6 +115,14 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
if (!EnableFuzzDeterminism()) {
SeedRandomForTest(SeedRand::FIXED_SEED);
}
// Reset globals
fDiscover = true;
fListen = true;
SetRPCWarmupStarting();
g_reachable_nets.Reset();
ClearLocal();
m_node.shutdown_signal = &m_interrupt;
m_node.shutdown_request = [this]{ return m_interrupt(); };
m_node.args = &gArgs;
@@ -214,7 +222,10 @@ BasicTestingSetup::~BasicTestingSetup()
} else {
fs::remove_all(m_path_root);
}
// Clear all arguments except for -datadir, which GUI tests currently rely
// on to be set even after the testing setup is destroyed.
gArgs.ClearArgs();
gArgs.ForceSetArg("-datadir", fs::PathToString(m_path_root));
}
ChainTestingSetup::ChainTestingSetup(const ChainType chainType, TestOpts opts)