mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-08 11:44:14 +01:00
refactor: De-globalize g_signals
This commit is contained in:
@@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
|
||||
const CBlock block_dummy;
|
||||
BlockValidationState state_dummy;
|
||||
while (generate) {
|
||||
GetMainSignals().BlockChecked(block_dummy, state_dummy);
|
||||
m_node.validation_signals->BlockChecked(block_dummy, state_dummy);
|
||||
}
|
||||
}};
|
||||
|
||||
@@ -37,8 +37,8 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
|
||||
// keep going for about 1 sec, which is 250k iterations
|
||||
for (int i = 0; i < 250000; i++) {
|
||||
auto sub = std::make_shared<TestSubscriberNoop>();
|
||||
RegisterSharedValidationInterface(sub);
|
||||
UnregisterSharedValidationInterface(sub);
|
||||
m_node.validation_signals->RegisterSharedValidationInterface(sub);
|
||||
m_node.validation_signals->UnregisterSharedValidationInterface(sub);
|
||||
}
|
||||
// tell the other thread we are done
|
||||
generate = false;
|
||||
@@ -52,8 +52,8 @@ BOOST_AUTO_TEST_CASE(unregister_validation_interface_race)
|
||||
class TestInterface : public CValidationInterface
|
||||
{
|
||||
public:
|
||||
TestInterface(std::function<void()> on_call = nullptr, std::function<void()> on_destroy = nullptr)
|
||||
: m_on_call(std::move(on_call)), m_on_destroy(std::move(on_destroy))
|
||||
TestInterface(CMainSignals& signals, std::function<void()> on_call = nullptr, std::function<void()> on_destroy = nullptr)
|
||||
: m_on_call(std::move(on_call)), m_on_destroy(std::move(on_destroy)), m_signals{signals}
|
||||
{
|
||||
}
|
||||
virtual ~TestInterface()
|
||||
@@ -64,14 +64,15 @@ public:
|
||||
{
|
||||
if (m_on_call) m_on_call();
|
||||
}
|
||||
static void Call()
|
||||
void Call()
|
||||
{
|
||||
CBlock block;
|
||||
BlockValidationState state;
|
||||
GetMainSignals().BlockChecked(block, state);
|
||||
m_signals.BlockChecked(block, state);
|
||||
}
|
||||
std::function<void()> m_on_call;
|
||||
std::function<void()> m_on_destroy;
|
||||
CMainSignals& m_signals;
|
||||
};
|
||||
|
||||
// Regression test to ensure UnregisterAllValidationInterfaces calls don't
|
||||
@@ -80,17 +81,23 @@ public:
|
||||
BOOST_AUTO_TEST_CASE(unregister_all_during_call)
|
||||
{
|
||||
bool destroyed = false;
|
||||
RegisterSharedValidationInterface(std::make_shared<TestInterface>(
|
||||
auto shared{std::make_shared<TestInterface>(
|
||||
*m_node.validation_signals,
|
||||
[&] {
|
||||
// First call should decrements reference count 2 -> 1
|
||||
UnregisterAllValidationInterfaces();
|
||||
m_node.validation_signals->UnregisterAllValidationInterfaces();
|
||||
BOOST_CHECK(!destroyed);
|
||||
// Second call should not decrement reference count 1 -> 0
|
||||
UnregisterAllValidationInterfaces();
|
||||
m_node.validation_signals->UnregisterAllValidationInterfaces();
|
||||
BOOST_CHECK(!destroyed);
|
||||
},
|
||||
[&] { destroyed = true; }));
|
||||
TestInterface::Call();
|
||||
[&] { destroyed = true; })};
|
||||
m_node.validation_signals->RegisterSharedValidationInterface(shared);
|
||||
BOOST_CHECK(shared.use_count() == 2);
|
||||
shared->Call();
|
||||
BOOST_CHECK(shared.use_count() == 1);
|
||||
BOOST_CHECK(!destroyed);
|
||||
shared.reset();
|
||||
BOOST_CHECK(destroyed);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user