From 693ed2061c003dcde2ee874003116abeed95b465 Mon Sep 17 00:00:00 2001 From: Alex Kremer Date: Tue, 7 Oct 2025 15:18:01 +0100 Subject: [PATCH] fix: ASAN issue with AmendmentBlockHandler test (#2674) Co-authored-by: Ayaz Salikhov --- src/etlng/AmendmentBlockHandlerInterface.hpp | 6 ++++++ src/etlng/impl/AmendmentBlockHandler.cpp | 15 +++++++++++++++ src/etlng/impl/AmendmentBlockHandler.hpp | 9 ++++----- tests/common/util/MockAmendmentBlockHandler.hpp | 1 + tests/unit/etl/AmendmentBlockHandlerTests.cpp | 2 +- tests/unit/etlng/AmendmentBlockHandlerTests.cpp | 3 ++- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/etlng/AmendmentBlockHandlerInterface.hpp b/src/etlng/AmendmentBlockHandlerInterface.hpp index 985d0e9c..2d7618c6 100644 --- a/src/etlng/AmendmentBlockHandlerInterface.hpp +++ b/src/etlng/AmendmentBlockHandlerInterface.hpp @@ -32,6 +32,12 @@ struct AmendmentBlockHandlerInterface { */ virtual void notifyAmendmentBlocked() = 0; + + /** + * @brief Stop the block handler from repeatedly executing + */ + virtual void + stop() = 0; }; } // namespace etlng diff --git a/src/etlng/impl/AmendmentBlockHandler.cpp b/src/etlng/impl/AmendmentBlockHandler.cpp index efb9fbc4..6e3e4776 100644 --- a/src/etlng/impl/AmendmentBlockHandler.cpp +++ b/src/etlng/impl/AmendmentBlockHandler.cpp @@ -25,6 +25,7 @@ #include #include +#include #include namespace etlng::impl { @@ -45,6 +46,11 @@ AmendmentBlockHandler::AmendmentBlockHandler( { } +AmendmentBlockHandler::~AmendmentBlockHandler() +{ + stop(); +} + void AmendmentBlockHandler::notifyAmendmentBlocked() { @@ -53,4 +59,13 @@ AmendmentBlockHandler::notifyAmendmentBlocked() operation_.emplace(ctx_.executeRepeatedly(interval_, action_)); } +void +AmendmentBlockHandler::stop() +{ + if (operation_.has_value()) { + operation_->abort(); + operation_.reset(); + } +} + } // namespace etlng::impl diff --git a/src/etlng/impl/AmendmentBlockHandler.hpp b/src/etlng/impl/AmendmentBlockHandler.hpp index 6275d4ea..f5c327d8 100644 --- a/src/etlng/impl/AmendmentBlockHandler.hpp +++ b/src/etlng/impl/AmendmentBlockHandler.hpp @@ -56,11 +56,10 @@ public: ActionType action = kDEFAULT_AMENDMENT_BLOCK_ACTION ); - ~AmendmentBlockHandler() override - { - if (operation_.has_value()) - operation_.value().abort(); - } + ~AmendmentBlockHandler() override; + + void + stop() override; void notifyAmendmentBlocked() override; diff --git a/tests/common/util/MockAmendmentBlockHandler.hpp b/tests/common/util/MockAmendmentBlockHandler.hpp index 6f1b1444..859d1393 100644 --- a/tests/common/util/MockAmendmentBlockHandler.hpp +++ b/tests/common/util/MockAmendmentBlockHandler.hpp @@ -25,4 +25,5 @@ struct MockAmendmentBlockHandler : etlng::AmendmentBlockHandlerInterface { MOCK_METHOD(void, notifyAmendmentBlocked, (), (override)); + MOCK_METHOD(void, stop, (), (override)); }; diff --git a/tests/unit/etl/AmendmentBlockHandlerTests.cpp b/tests/unit/etl/AmendmentBlockHandlerTests.cpp index dee8d95d..85a2b8f8 100644 --- a/tests/unit/etl/AmendmentBlockHandlerTests.cpp +++ b/tests/unit/etl/AmendmentBlockHandlerTests.cpp @@ -36,7 +36,7 @@ struct AmendmentBlockHandlerTest : util::prometheus::WithPrometheus, SyncAsioCon etl::SystemState state; }; -TEST_F(AmendmentBlockHandlerTest, CallTonotifyAmendmentBlockedSetsStateAndRepeatedlyCallsAction) +TEST_F(AmendmentBlockHandlerTest, CallToNotifyAmendmentBlockedSetsStateAndRepeatedlyCallsAction) { AmendmentBlockHandler handler{ctx_, state, std::chrono::nanoseconds{1}, actionMock.AsStdFunction()}; diff --git a/tests/unit/etlng/AmendmentBlockHandlerTests.cpp b/tests/unit/etlng/AmendmentBlockHandlerTests.cpp index c77c4f7f..74cfd894 100644 --- a/tests/unit/etlng/AmendmentBlockHandlerTests.cpp +++ b/tests/unit/etlng/AmendmentBlockHandlerTests.cpp @@ -40,7 +40,7 @@ protected: util::async::CoroExecutionContext ctx_; }; -TEST_F(AmendmentBlockHandlerNgTests, CallTonotifyAmendmentBlockedSetsStateAndRepeatedlyCallsAction) +TEST_F(AmendmentBlockHandlerNgTests, CallToNotifyAmendmentBlockedSetsStateAndRepeatedlyCallsAction) { static constexpr auto kMAX_ITERATIONS = 10uz; etlng::impl::AmendmentBlockHandler handler{ctx_, state_, std::chrono::nanoseconds{1}, actionMock_.AsStdFunction()}; @@ -55,6 +55,7 @@ TEST_F(AmendmentBlockHandlerNgTests, CallTonotifyAmendmentBlockedSetsStateAndRep handler.notifyAmendmentBlocked(); stop.acquire(); // wait for the counter to reach over kMAX_ITERATIONS + handler.stop(); EXPECT_TRUE(state_.isAmendmentBlocked); }