diff --git a/Tests/unit/core/CMakeLists.txt b/Tests/unit/core/CMakeLists.txt index 79d28d528..d76face19 100644 --- a/Tests/unit/core/CMakeLists.txt +++ b/Tests/unit/core/CMakeLists.txt @@ -70,7 +70,7 @@ add_executable(${TEST_RUNNER_NAME} test_thread.cpp test_threadpool.cpp test_time.cpp - #test_timer.cpp + test_timer.cpp test_tristate.cpp #test_valuerecorder.cpp test_weblinkjson.cpp diff --git a/Tests/unit/core/test_timer.cpp b/Tests/unit/core/test_timer.cpp index a0e29dbb2..b15d47ea0 100644 --- a/Tests/unit/core/test_timer.cpp +++ b/Tests/unit/core/test_timer.cpp @@ -40,23 +40,27 @@ namespace Tests { public: uint64_t Timed(const uint64_t scheduledTime) { - if (!_timerDone) { - Core::Time nextTick = Core::Time::Now(); - uint32_t time = 100; // 0.1 second - nextTick.Add(time); - std::unique_lock lk(_mutex); - _timerDone++; - _cv.notify_one(); - return nextTick.Ticks(); - } - std::unique_lock lk(_mutex); + constexpr uint32_t time = 100; // 0.1 second + + std::unique_lock lock(_mutex); + _timerDone++; + + lock.unlock(); + _cv.notify_one(); - return 0; + + Core::Time nextTick = Core::Time::Now() + time; + + return nextTick.Ticks(); } static int GetCount() { + std::unique_lock lk(_mutex); + + _cv.wait(lk); + return _timerDone; } @@ -80,7 +84,7 @@ namespace Tests { WatchDogHandler& operator=(const WatchDogHandler&) = delete; WatchDogHandler() : BaseClass(Core::Thread::DefaultStackSize(), _T("WatchDogTimer"), *this) - , _event(false, false) + , _event(false, true) { } ~WatchDogHandler() @@ -108,26 +112,30 @@ namespace Tests { mutable Core::Event _event; }; - TEST(DISABLED_Core_Timer, LoopedTimer) + TEST(Core_Timer, LoopedTimer) { + constexpr uint32_t time = 100; + Core::TimerType timer(Core::Thread::DefaultStackSize(), _T("LoopedTimer")); - uint32_t time = 100; - Core::Time nextTick = Core::Time::Now(); - nextTick.Add(time); + Core::Time nextTick = Core::Time::Now() + time; + timer.Schedule(nextTick.Ticks(), TimeHandler()); - std::unique_lock lk(TimeHandler::_mutex); - while (!(TimeHandler::GetCount() == 2)) { - TimeHandler::_cv.wait(lk); + + while (TimeHandler::GetCount() <= 2) { } + + timer.Flush(); } TEST(Core_Timer, QueuedTimer) { + constexpr uint32_t time = 100; + Core::TimerType timer(Core::Thread::DefaultStackSize(), _T("QueuedTimer")); - uint32_t time = 100; Core::Time nextTick = Core::Time::Now(); + nextTick.Add(time); timer.Schedule(nextTick.Ticks(), TimeHandler()); @@ -136,24 +144,31 @@ namespace Tests { nextTick.Add(3 * time); timer.Schedule(nextTick.Ticks(), TimeHandler()); - std::unique_lock lk(TimeHandler::_mutex); - while (!(TimeHandler::GetCount() == 5)) { - TimeHandler::_cv.wait(lk); + + while (TimeHandler::GetCount() <= 5) { } + + timer.Flush(); } TEST(Core_Timer, PastTime) { + constexpr uint32_t time = 100; + Core::TimerType timer(Core::Thread::DefaultStackSize(), _T("PastTime")); - uint32_t time = 100; // 0.1 second Core::Time pastTime = Core::Time::Now(); + + ASSERT_GT(pastTime.Ticks() / 1000, 0); + pastTime.Sub(time); + timer.Schedule(pastTime.Ticks(), TimeHandler()); - std::unique_lock lk(TimeHandler::_mutex); - while (!(TimeHandler::GetCount() == 6)) { - TimeHandler::_cv.wait(lk); + + while (TimeHandler::GetCount() <= 6) { } + + timer.Flush(); } TEST(Core_Timer, WatchDogType)