Skip to content

Commit

Permalink
[Tests/unit/core] : Fix 'test_timer'. (#1599)
Browse files Browse the repository at this point in the history
Co-authored-by: MFransen69 <39826971+MFransen69@users.noreply.github.com>
Co-authored-by: Pierre Wielders <pierre@wielders.net>
  • Loading branch information
3 people committed May 17, 2024
1 parent 9e1c852 commit f8e5fe5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Tests/unit/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 42 additions & 27 deletions Tests/unit/core/test_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lk(_mutex);
_timerDone++;
_cv.notify_one();
return nextTick.Ticks();
}
std::unique_lock<std::mutex> lk(_mutex);
constexpr uint32_t time = 100; // 0.1 second

std::unique_lock<std::mutex> 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<std::mutex> lk(_mutex);

_cv.wait(lk);

return _timerDone;
}

Expand All @@ -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()
Expand Down Expand Up @@ -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<TimeHandler> 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<std::mutex> 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<TimeHandler> timer(Core::Thread::DefaultStackSize(), _T("QueuedTimer"));
uint32_t time = 100;

Core::Time nextTick = Core::Time::Now();

nextTick.Add(time);
timer.Schedule(nextTick.Ticks(), TimeHandler());

Expand All @@ -136,24 +144,31 @@ namespace Tests {

nextTick.Add(3 * time);
timer.Schedule(nextTick.Ticks(), TimeHandler());
std::unique_lock<std::mutex> 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<TimeHandler> 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<std::mutex> lk(TimeHandler::_mutex);
while (!(TimeHandler::GetCount() == 6)) {
TimeHandler::_cv.wait(lk);

while (TimeHandler::GetCount() <= 6) {
}

timer.Flush();
}

TEST(Core_Timer, WatchDogType)
Expand Down

0 comments on commit f8e5fe5

Please sign in to comment.