libcron/test/CronTest.cpp

190 lines
5.3 KiB
C++
Raw Normal View History

2018-03-11 06:42:00 +08:00
#include <catch.hpp>
#include <libcron/Cron.h>
#include <thread>
#include <iostream>
2018-03-11 06:42:00 +08:00
using namespace libcron;
using namespace std::chrono;
2018-03-12 06:38:12 +08:00
std::string create_schedule_expiring_in(std::shared_ptr<ICronClock> clock, hours h, minutes m, seconds s)
2018-03-11 06:42:00 +08:00
{
2018-03-12 06:38:12 +08:00
auto now = clock->now() + h + m + s;
2018-03-11 06:42:00 +08:00
auto dt = CronSchedule::to_calendar_time(now);
std::string res{};
2018-03-11 06:42:00 +08:00
res += std::to_string(dt.sec) + " ";
res += std::to_string(dt.min) + " ";
res += std::to_string(dt.hour) + " * * ?";
2018-03-11 06:42:00 +08:00
return res;
}
SCENARIO("Adding a task")
{
GIVEN("A Cron instance with no task")
{
Cron c;
auto expired = false;
2018-03-11 06:42:00 +08:00
THEN("Starts with no task")
{
REQUIRE(c.count() == 0);
}
WHEN("Adding a task that runs every second")
{
REQUIRE(c.add_schedule("A task", "* * * * * ?",
[&expired]()
2018-03-11 06:42:00 +08:00
{
expired = true;
2018-03-11 06:42:00 +08:00
})
);
THEN("Count is 1 and task was not expired two seconds ago")
{
REQUIRE(c.count() == 1);
2018-03-12 03:09:06 +08:00
c.execute_expired_tasks(c.get_clock()->now() - 2s);
REQUIRE_FALSE(expired);
2018-03-11 06:42:00 +08:00
}
AND_THEN("Task is expired when calculating based on current time")
{
c.execute_expired_tasks();
2018-03-11 06:42:00 +08:00
THEN("Task is expired")
{
REQUIRE(expired);
2018-03-11 06:42:00 +08:00
}
}
}
}
}
SCENARIO("Adding a task that expires in the future")
{
GIVEN("A Cron instance with task expiring in 3 seconds")
{
auto expired = false;
2018-03-11 06:42:00 +08:00
Cron c;
2018-03-12 06:38:12 +08:00
REQUIRE(c.add_schedule("A task", create_schedule_expiring_in(c.get_clock(), hours{0}, minutes{0}, seconds{3}),
[&expired]()
2018-03-11 06:42:00 +08:00
{
expired = true;
2018-03-11 06:42:00 +08:00
})
);
THEN("Not yet expired")
{
REQUIRE_FALSE(expired);
2018-03-11 06:42:00 +08:00
}
AND_WHEN("When waiting one second")
{
std::this_thread::sleep_for(1s);
c.execute_expired_tasks();
2018-03-11 06:42:00 +08:00
THEN("Task has not yet expired")
{
REQUIRE_FALSE(expired);
2018-03-11 06:42:00 +08:00
}
}
AND_WHEN("When waiting three seconds")
{
std::this_thread::sleep_for(3s);
c.execute_expired_tasks();
2018-03-11 06:42:00 +08:00
THEN("Task has expired")
{
REQUIRE(expired);
2018-03-11 06:42:00 +08:00
}
}
}
}
SCENARIO("Task priority")
{
GIVEN("A Cron instance with two tasks expiring in 3 and 5 seconds, added in 'reverse' order")
{
auto _3_second_expired = 0;
auto _5_second_expired = 0;
Cron c;
2018-03-12 06:38:12 +08:00
REQUIRE(c.add_schedule("Five", create_schedule_expiring_in(c.get_clock(), hours{0}, minutes{0}, seconds{5}),
[&_5_second_expired]()
{
_5_second_expired++;
})
);
2018-03-12 06:38:12 +08:00
REQUIRE(c.add_schedule("Three", create_schedule_expiring_in(c.get_clock(), hours{0}, minutes{0}, seconds{3}),
[&_3_second_expired]()
{
_3_second_expired++;
})
);
THEN("Not yet expired")
{
REQUIRE_FALSE(_3_second_expired);
REQUIRE_FALSE(_5_second_expired);
}
WHEN("Waiting 1 seconds")
{
std::this_thread::sleep_for(1s);
c.execute_expired_tasks();
THEN("Task has not yet expired")
{
REQUIRE(_3_second_expired == 0);
REQUIRE(_5_second_expired == 0);
}
}
AND_WHEN("Waiting 3 seconds")
{
std::this_thread::sleep_for(3s);
c.execute_expired_tasks();
THEN("3 second task has expired")
{
REQUIRE(_3_second_expired == 1);
REQUIRE(_5_second_expired == 0);
}
}
AND_WHEN("Waiting 5 seconds")
{
std::this_thread::sleep_for(5s);
c.execute_expired_tasks();
THEN("3 and 5 second task has expired")
{
REQUIRE(_3_second_expired == 1);
REQUIRE(_5_second_expired == 1);
}
}
AND_WHEN("Waiting based on the time given by the Cron instance")
{
std::this_thread::sleep_for(c.time_until_next());
c.execute_expired_tasks();
THEN("3 second task has expired")
{
REQUIRE(_3_second_expired == 1);
REQUIRE(_5_second_expired == 0);
}
}
AND_WHEN("Waiting based on the time given by the Cron instance")
{
std::this_thread::sleep_for(c.time_until_next());
REQUIRE(c.execute_expired_tasks() == 1);
std::this_thread::sleep_for(c.time_until_next());
REQUIRE(c.execute_expired_tasks() == 1);
THEN("3 and 5 second task has each expired once")
{
REQUIRE(_3_second_expired == 1);
REQUIRE(_5_second_expired == 1);
}
}
}
}