mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
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:
parent
197b2aaaaa
commit
b18f78fb1e
8 changed files with 88 additions and 15 deletions
|
@ -589,6 +589,14 @@ void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
|
|||
}
|
||||
}
|
||||
|
||||
void ArgsManager::ClearArgs()
|
||||
{
|
||||
LOCK(cs_args);
|
||||
m_settings = {};
|
||||
m_available_args.clear();
|
||||
m_network_only_args.clear();
|
||||
}
|
||||
|
||||
void ArgsManager::CheckMultipleCLIArgs() const
|
||||
{
|
||||
LOCK(cs_args);
|
||||
|
|
|
@ -359,11 +359,7 @@ protected:
|
|||
/**
|
||||
* Clear available arguments
|
||||
*/
|
||||
void ClearArgs() {
|
||||
LOCK(cs_args);
|
||||
m_available_args.clear();
|
||||
m_network_only_args.clear();
|
||||
}
|
||||
void ClearArgs();
|
||||
|
||||
/**
|
||||
* Check CLI command args
|
||||
|
|
|
@ -121,6 +121,13 @@ public:
|
|||
m_reachable.clear();
|
||||
}
|
||||
|
||||
void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
AssertLockNotHeld(m_mutex);
|
||||
LOCK(m_mutex);
|
||||
m_reachable = DefaultNets();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
AssertLockNotHeld(m_mutex);
|
||||
|
@ -142,17 +149,21 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
mutable Mutex m_mutex;
|
||||
|
||||
std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){
|
||||
NET_UNROUTABLE,
|
||||
NET_IPV4,
|
||||
NET_IPV6,
|
||||
NET_ONION,
|
||||
NET_I2P,
|
||||
NET_CJDNS,
|
||||
NET_INTERNAL
|
||||
static std::unordered_set<Network> DefaultNets()
|
||||
{
|
||||
return {
|
||||
NET_UNROUTABLE,
|
||||
NET_IPV4,
|
||||
NET_IPV6,
|
||||
NET_ONION,
|
||||
NET_I2P,
|
||||
NET_CJDNS,
|
||||
NET_INTERNAL
|
||||
};
|
||||
};
|
||||
|
||||
mutable Mutex m_mutex;
|
||||
std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){DefaultNets()};
|
||||
};
|
||||
|
||||
extern ReachableNets g_reachable_nets;
|
||||
|
|
|
@ -323,6 +323,12 @@ void SetRPCWarmupStatus(const std::string& newStatus)
|
|||
rpcWarmupStatus = newStatus;
|
||||
}
|
||||
|
||||
void SetRPCWarmupStarting()
|
||||
{
|
||||
LOCK(g_rpc_warmup_mutex);
|
||||
fRPCInWarmup = true;
|
||||
}
|
||||
|
||||
void SetRPCWarmupFinished()
|
||||
{
|
||||
LOCK(g_rpc_warmup_mutex);
|
||||
|
|
|
@ -30,6 +30,7 @@ void RpcInterruptionPoint();
|
|||
*/
|
||||
void SetRPCWarmupStatus(const std::string& newStatus);
|
||||
/* Mark warmup as done. RPC calls will be processed from now on. */
|
||||
void SetRPCWarmupStarting();
|
||||
void SetRPCWarmupFinished();
|
||||
|
||||
/* returns the current warmup state. */
|
||||
|
|
|
@ -63,6 +63,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
|
||||
|
|
|
@ -702,6 +702,7 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
|
||||
{
|
||||
g_reachable_nets.Reset();
|
||||
BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
|
||||
BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
|
||||
BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
|
||||
|
|
49
src/test/node_init_tests.cpp
Normal file
49
src/test/node_init_tests.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
// 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)
|
||||
{
|
||||
// Reset logging, config file path, rpc state, reachable nets to avoid errors in AppInitMain
|
||||
LogInstance().DisconnectTestLogger();
|
||||
m_node.args->SetConfigFilePath({});
|
||||
SetRPCWarmupStarting();
|
||||
g_reachable_nets.Reset();
|
||||
|
||||
// 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()
|
Loading…
Add table
Reference in a new issue