From 3611f538242c8cd5978611673da2fb7cf54aa095 Mon Sep 17 00:00:00 2001 From: "Addisu Z. Taddese" Date: Tue, 20 Aug 2024 22:52:09 -0500 Subject: [PATCH] Add rapid fire test Signed-off-by: Addisu Z. Taddese --- src/SignalHandler_TEST.cc | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/SignalHandler_TEST.cc b/src/SignalHandler_TEST.cc index 246b86c7..5df2a9d5 100644 --- a/src/SignalHandler_TEST.cc +++ b/src/SignalHandler_TEST.cc @@ -291,3 +291,39 @@ TEST(SignalHandler, MultipleThreads) for (int i = 0; i < threadCount; ++i) EXPECT_EQ(SIGINT, results[i]); } + +///////////////////////////////////////////////// +TEST(SignalHandler, RapidFire) +{ + resetSignals(); + std::condition_variable cv; + std::mutex countMutex; + int countHandlerCalls = 0; + constexpr int kNumSignals = 100; + auto cb = [&](int _sig) + { + if (_sig == SIGTERM) + { + std::lock_guard lk(countMutex); + ++countHandlerCalls; + if (countHandlerCalls >= kNumSignals) + { + cv.notify_one(); + } + } + }; + common::SignalHandler handler1; + EXPECT_TRUE(handler1.AddCallback(cb)); + + for (int i=0; i < kNumSignals; ++i) + { + std::raise(SIGTERM); + std::this_thread::sleep_for(std::chrono::microseconds(100)); + } + + // wait for callback to be called kNumSignal times with a timeout + std::unique_lock lk(countMutex); + cv.wait_for(lk, std::chrono::seconds(5), + [&] { return countHandlerCalls >= kNumSignals; }); + EXPECT_GE(countHandlerCalls, kNumSignals); +}