From 6b96d217325463b582dac4828ca6bd8e627a5199 Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Fri, 28 Feb 2020 18:44:47 +0000 Subject: [PATCH] extension of the timer test to check safe running timer erasure --- lib/test/common/timer_test.cc | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/test/common/timer_test.cc b/lib/test/common/timer_test.cc index 6fde27052..909745092 100644 --- a/lib/test/common/timer_test.cc +++ b/lib/test/common/timer_test.cc @@ -367,6 +367,51 @@ int timers_test5() return SRSLTE_SUCCESS; } +/** + * Description: Check if erasure of a running timer is safe + */ +int timers_test6() +{ + timer_handler timers; + + std::vector vals; + + // Event: Add a timer that gets erased 1 tti after. + { + timer_handler::unique_timer t = timers.get_unique_timer(); + t.set(2, [&vals](uint32_t tid) { vals.push_back(1); }); + t.run(); + TESTASSERT(timers.nof_running_timers() == 1); + timers.step_all(); + } + TESTASSERT(timers.nof_running_timers() == 0); + TESTASSERT(timers.nof_timers() == 0); + + // TEST: The timer callback should not have been called + timers.step_all(); + TESTASSERT(vals.empty()); + + // Event: Add a timer that gets erased right after, and add another timer with same timeout + { + timer_handler::unique_timer t = timers.get_unique_timer(); + t.set(2, [&vals](uint32_t tid) { vals.push_back(2); }); + t.run(); + TESTASSERT(timers.nof_running_timers() == 1); + timers.step_all(); + TESTASSERT(t.time_elapsed() == 1); + } + timer_handler::unique_timer t = timers.get_unique_timer(); + t.set(1, [&vals](uint32_t tid) { vals.push_back(3); }); + t.run(); + TESTASSERT(timers.nof_running_timers() == 1); + + // TEST: The second timer's callback should be the one being called, and should be called only once + timers.step_all(); + TESTASSERT(vals.size() == 1 and vals[0] == 3); + + return SRSLTE_SUCCESS; +} + int main() { TESTASSERT(timers_test1() == SRSLTE_SUCCESS); @@ -374,6 +419,7 @@ int main() TESTASSERT(timers_test3() == SRSLTE_SUCCESS); TESTASSERT(timers_test4() == SRSLTE_SUCCESS); TESTASSERT(timers_test5() == SRSLTE_SUCCESS); + TESTASSERT(timers_test6() == SRSLTE_SUCCESS); printf("Success\n"); return 0; }