Skip to content

Commit

Permalink
priority queue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomLonergan03 committed Aug 19, 2023
1 parent cd81fb0 commit 8d5a7bf
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions test/core/scheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <gtest/gtest.h>

#include <core/logger.hpp>
#include <core/scheduler.hpp>
#include <utils/manual_time.hpp>

namespace hyped::test {

TEST(Scheduler, noTasks)
{
utils::ManualTime manual_time;
core::Logger logger("test", core::LogLevel::kDebug, manual_time);
core::Scheduler scheduler(logger, manual_time);
ASSERT_EQ(scheduler.run(), core::Result::kSuccess);
}

TEST(Scheduler, immediateTask)
{
utils::ManualTime manual_time;
core::Logger logger("test", core::LogLevel::kDebug, manual_time);
core::Scheduler scheduler(logger, manual_time);
bool task_called = false;
scheduler.addTask(
[&task_called]() {
task_called = true;
return core::Result::kSuccess;
},
0);
ASSERT_EQ(scheduler.run(), core::Result::kSuccess);
ASSERT_TRUE(task_called);
}

TEST(Scheduler, delayedTask)
{
utils::ManualTime manual_time;
core::Logger logger("test", core::LogLevel::kDebug, manual_time);
core::Scheduler scheduler(logger, manual_time);
bool task_called = false;
scheduler.addTask(
[&task_called]() {
task_called = true;
return core::Result::kSuccess;
},
1000);
ASSERT_EQ(scheduler.run(), core::Result::kSuccess);
ASSERT_FALSE(task_called);
manual_time.set_time(std::chrono::system_clock::from_time_t(1));
ASSERT_EQ(scheduler.run(), core::Result::kSuccess);
ASSERT_TRUE(task_called);
}

TEST(Scheduler, failingTask)
{
utils::ManualTime manual_time;
core::Logger logger("test", core::LogLevel::kDebug, manual_time);
core::Scheduler scheduler(logger, manual_time);
bool task_called = false;
scheduler.addTask(
[&task_called]() {
task_called = true;
return core::Result::kFailure;
},
0);
ASSERT_EQ(scheduler.run(), core::Result::kFailure);
ASSERT_TRUE(task_called);
}

TEST(Scheduler, multipleTasksCorrectOrder)
{
utils::ManualTime manual_time;
core::Logger logger("test", core::LogLevel::kDebug, manual_time);
core::Scheduler scheduler(logger, manual_time);
bool task1_called = false;
bool task2_called = false;
scheduler.addTask(
[&task1_called]() {
task1_called = true;
return core::Result::kSuccess;
},
0);
scheduler.addTask(
[&task2_called]() {
task2_called = true;
return core::Result::kSuccess;
},
0);
manual_time.set_time(std::chrono::system_clock::from_time_t(1));
ASSERT_EQ(scheduler.run(), core::Result::kSuccess);
ASSERT_TRUE(task1_called);
ASSERT_EQ(scheduler.run(), core::Result::kSuccess);
ASSERT_TRUE(task2_called);
}
} // namespace hyped::test

0 comments on commit 8d5a7bf

Please sign in to comment.