5 Commits

Author SHA1 Message Date
7ef39558a1 Feature/add executed on time check (#7)
* Initial commit on executed on time feature. A task was executed on time if the function call happened within one second since it expired.

* Adding tests, fixing some errors.

* Using recursirve mutex to allowing to call safely call was_executed_on_time in an Mt-environment

* Changing from boolean expression to get_delay, being even more flexibel

* Cleanup

* Adding dedicated TaskContext

* Changing to Interface-Class Approach

* Renaming to TaskInformation, making it pure virtual

* Removing unnecessary Proxy-Class

* Cleaning up

* Passing a const reference instead of a pointer to avoid nullptr checks in the callback

* Cleaning up add_schedule.

* Adding TaskInformation API to readme.

Co-authored-by: Heinz-Peter Liechtenecker <h.liechtenecker@fh-kaernten.at>
2020-09-10 19:03:50 +02:00
76da315c13 Adding remove-feature to Cron-Class (#6)
* Adding functions to remove a specific schedule (by the given name) or all scheduled tasks from the Cron class.

* Update libcron/include/libcron/Task.h

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>

* Update libcron/include/libcron/Task.h

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>

* Update libcron/include/libcron/Cron.h

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>

* Update libcron/include/libcron/Cron.h

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>

* Update libcron/include/libcron/Cron.h

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>

* Adding Multithreading support via template, adding documentation

* Apply suggestions from code review

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>

* Finishing suggestions from code-review (renaming elements)

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>
2020-09-02 15:57:14 +02:00
440f5099ba Use c_encoding function for datatype conversion (#5)
* Use c_encoding function 

The MSVC compiler does not accept direct conversion from weekday datatype (used in the date.h dependency) to unsigned int used in the DayOfWeek Enum. However, the c_encoding function gives the necessary datatype conversion.

* Updated submodule libcron/externals/date

Co-authored-by: Heinz-Peter Liechtenecker <h.liechtenecker@fh-kaernten.at>
2020-06-30 19:07:58 +02:00
b82267acca Merge pull request #3 from PerMalmberg/feature/random-via-textual-names
Implemented support for using textual names in randomization.
2019-05-17 15:27:28 +02:00
d61086f69e Implemented support for using textual names in randomization. 2019-05-17 13:39:32 +02:00
17 changed files with 529 additions and 64 deletions

View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.6) cmake_minimum_required(VERSION 3.6)
project(top)
add_subdirectory(libcron) add_subdirectory(libcron)
add_subdirectory(test) add_subdirectory(test)

View File

@ -1,7 +1,89 @@
# libcron # libcron
A C++ scheduling library using cron formatting. A C++ scheduling library using cron formatting.
# Local time vs UTC # Using the Scheduler
Libcron offers an easy to use API to add callbacks with corresponding cron-formatted strings:
```
libcron::Cron cron;
cron.add_schedule("Hello from Cron", "* * * * * ?", [=](auto&) {
std::cout << "Hello from libcron!" std::endl;
});
```
To trigger the execution of callbacks, one must call `libcron::Cron::tick` at least once a second to prevent missing schedules:
```
while(true)
{
cron.tick();
std::this_thread::sleep_for(500mS);
}
```
The callback must have the following signature:
```
std::function<void(const libcron::TaskInformation&)>
```
`libcron::Taskinformation` offers a convenient API to retrieve further information:
- `libcron::TaskInformation::get_delay` informs about the delay between planned and actual execution of the callback. Hence, it is possible to ensure that a task was executed within a specific tolerance:
```
libcron::Cron cron;
cron.add_schedule("Hello from Cron", "* * * * * ?", [=](auto& i) {
using namespace std::chrono_literals;
if (i.get_delay() >= 1s)
{
std::cout << "The Task was executed too late..." << std::endl;
}
});
```
## Removing schedules from `libcron::Cron`
libcron::Cron offers two convenient functions to remove schedules:
- `clear_schedules()` will remove all schedules
- `remove_schedule(std::string)` will remove a specific schedule
For example, `cron.remove_schedule("Hello from Cron")` will remove the previously added task.
## Removing/Adding tasks at runtime in a multithreaded environment
When Calling `libcron::Cron::tick` from another thread than `add_schedule`, `clear_schedule` and `remove_schedule`, one must take care to protect the internal resources of `libcron::Cron` so that tasks are not removed or added while `libcron::Cron` is iterating over the schedules. `libcron::Cron` can take care of that, you simply have to define your own aliases:
```
/* The default class uses NullLock, which does not lock the resources at runtime */
template<typename ClockType = libcron::LocalClock, typename LockType = libcron::NullLock>
class Cron
{
...
}
/* Define an alias for a thread-safe Cron scheduler which automatically locks ressources when needed */
using CronMt = libcron::Cron<libcron::LocalClock, libcron::Locker>
CronMt cron;
cron.add_schedule("Hello from Cron", "* * * * * ?", [=]() {
std::cout << "Hello from CronMt!" std::endl;
});
....
```
However, this comes with costs: Whenever you call `tick`, a `std::mutex` will be locked and unlocked. So only use the `libcron::Locker` to protect resources when you really need too.
## Local time vs UTC
This library uses `std::chrono::system_clock::timepoint` as its time unit. While that is UTC by default, the Cron-class This library uses `std::chrono::system_clock::timepoint` as its time unit. While that is UTC by default, the Cron-class
uses a `LocalClock` by default which offsets `system_clock::now()` by the current UTC-offset. If you wish to work in uses a `LocalClock` by default which offsets `system_clock::now()` by the current UTC-offset. If you wish to work in
@ -55,6 +137,7 @@ Each part is separated by one or more whitespaces. It is thus important to keep
* Invalid: * Invalid:
* 0, 3, 40-50 * * * * ? * 0, 3, 40-50 * * * * ?
`Day of month` and `day of week` are mutually exclusive so one of them must at always be ignored using `Day of month` and `day of week` are mutually exclusive so one of them must at always be ignored using
the '?'-character to ensure that it is not possible to specify a statement which results in an impossible mix of these fields. the '?'-character to ensure that it is not possible to specify a statement which results in an impossible mix of these fields.
@ -71,8 +154,8 @@ the '?'-character to ensure that it is not possible to specify a statement which
The standard cron format does not allow for randomization, but with the use of `CronRandomization` you can generate random The standard cron format does not allow for randomization, but with the use of `CronRandomization` you can generate random
schedules using the following format: `R(range_start-range_end)`, where `range_start` and `range_end` follow the same rules schedules using the following format: `R(range_start-range_end)`, where `range_start` and `range_end` follow the same rules
as for a regular cron range with the addition that only numbers are allowed. All the rules for a regular cron expression as for a regular cron range (step-syntax is not supported). All the rules for a regular cron expression still applies
still applies when using randomization, i.e. mutual exclusiveness and not extra spaces. when using randomization, i.e. mutual exclusiveness and no extra spaces.
## Examples ## Examples
|Expression | Meaning |Expression | Meaning
@ -80,9 +163,9 @@ still applies when using randomization, i.e. mutual exclusiveness and not extra
| 0 0 R(13-20) * * ? | On the hour, on a random hour 13-20, inclusive. | 0 0 R(13-20) * * ? | On the hour, on a random hour 13-20, inclusive.
| 0 0 0 ? * R(0-6) | A random weekday, every week, at midnight. | 0 0 0 ? * R(0-6) | A random weekday, every week, at midnight.
| 0 R(45-15) */12 ? * * | A random minute between 45-15, inclusive, every 12 hours. | 0 R(45-15) */12 ? * * | A random minute between 45-15, inclusive, every 12 hours.
|0 0 0 ? R(DEC-MAR) R(SAT-SUN)| On the hour, on a random month december to march, on a random weekday saturday to sunday.
# Used Third party libraries # Used Third party libraries
Howard Hinnant's [date libraries](https://github.com/HowardHinnant/date/) Howard Hinnant's [date libraries](https://github.com/HowardHinnant/date/)

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.6) cmake_minimum_required(VERSION 3.6)
project(libcron) project(libcron)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
if( MSVC ) if( MSVC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")

View File

@ -4,23 +4,41 @@
#include <chrono> #include <chrono>
#include <queue> #include <queue>
#include <memory> #include <memory>
#include <mutex>
#include "Task.h" #include "Task.h"
#include "CronClock.h" #include "CronClock.h"
namespace libcron namespace libcron
{ {
template<typename ClockType> class NullLock
{
public:
void lock() {}
void unlock() {}
};
class Locker
{
public:
void lock() { m.lock(); }
void unlock() { m.unlock(); }
private:
std::recursive_mutex m{};
};
template<typename ClockType, typename LockType>
class Cron; class Cron;
template<typename ClockType> template<typename ClockType, typename LockType>
std::ostream& operator<<(std::ostream& stream, const Cron<ClockType>& c); std::ostream& operator<<(std::ostream& stream, const Cron<ClockType, LockType>& c);
template<typename ClockType = libcron::LocalClock> template<typename ClockType = libcron::LocalClock, typename LockType = libcron::NullLock>
class Cron class Cron
{ {
public: public:
bool add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work);
bool add_schedule(std::string name, const std::string& schedule, std::function<void()> work); void clear_schedules();
void remove_schedule(const std::string& name);
size_t count() const size_t count() const
{ {
@ -48,7 +66,7 @@ namespace libcron
void get_time_until_expiry_for_tasks( void get_time_until_expiry_for_tasks(
std::vector<std::tuple<std::string, std::chrono::system_clock::duration>>& status) const; std::vector<std::tuple<std::string, std::chrono::system_clock::duration>>& status) const;
friend std::ostream& operator<<<>(std::ostream& stream, const Cron<ClockType>& c); friend std::ostream& operator<<<>(std::ostream& stream, const Cron<ClockType, LockType>& c);
private: private:
class Queue class Queue
@ -66,6 +84,50 @@ namespace libcron
{ {
return c; return c;
} }
void clear()
{
lock.lock();
while (!empty())
pop();
lock.unlock();
}
template<typename T>
void remove(T to_remove)
{
lock.lock();
/* Copy current elements */
std::vector<Task> temp{};
std::swap(temp, c);
/* Refill with elements ensuring correct order by calling push */
for (const auto& task : temp)
{
if (task != to_remove)
push(task);
}
lock.unlock();
}
void lock_queue()
{
/* Do not allow to manipulate the Queue */
lock.lock();
}
void release_queue()
{
/* Allow Access to the Queue Manipulating-Functions */
lock.unlock();
}
private:
LockType lock;
}; };
@ -75,25 +137,39 @@ namespace libcron
std::chrono::system_clock::time_point last_tick{}; std::chrono::system_clock::time_point last_tick{};
}; };
template<typename ClockType> template<typename ClockType, typename LockType>
bool Cron<ClockType>::add_schedule(std::string name, const std::string& schedule, std::function<void()> work) bool Cron<ClockType, LockType>::add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work)
{ {
auto cron = CronData::create(schedule); auto cron = CronData::create(schedule);
bool res = cron.is_valid(); bool res = cron.is_valid();
if (res) if (res)
{ {
Task t{std::move(name), CronSchedule{cron}, std::move(work)}; tasks.lock_queue();
Task t{std::move(name), CronSchedule{cron}, work };
if (t.calculate_next(clock.now())) if (t.calculate_next(clock.now()))
{ {
tasks.push(t); tasks.push(t);
} }
tasks.release_queue();
} }
return res; return res;
} }
template<typename ClockType> template<typename ClockType, typename LockType>
std::chrono::system_clock::duration Cron<ClockType>::time_until_next() const void Cron<ClockType, LockType>::clear_schedules()
{
tasks.clear();
}
template<typename ClockType, typename LockType>
void Cron<ClockType, LockType>::remove_schedule(const std::string& name)
{
tasks.remove(name);
}
template<typename ClockType, typename LockType>
std::chrono::system_clock::duration Cron<ClockType, LockType>::time_until_next() const
{ {
std::chrono::system_clock::duration d{}; std::chrono::system_clock::duration d{};
if (tasks.empty()) if (tasks.empty())
@ -108,9 +184,10 @@ namespace libcron
return d; return d;
} }
template<typename ClockType> template<typename ClockType, typename LockType>
size_t Cron<ClockType>::tick(std::chrono::system_clock::time_point now) size_t Cron<ClockType, LockType>::tick(std::chrono::system_clock::time_point now)
{ {
tasks.lock_queue();
size_t res = 0; size_t res = 0;
if(!first_tick) if(!first_tick)
@ -189,11 +266,12 @@ namespace libcron
} }
}); });
tasks.release_queue();
return res; return res;
} }
template<typename ClockType> template<typename ClockType, typename LockType>
void Cron<ClockType>::get_time_until_expiry_for_tasks(std::vector<std::tuple<std::string, void Cron<ClockType, LockType>::get_time_until_expiry_for_tasks(std::vector<std::tuple<std::string,
std::chrono::system_clock::duration>>& status) const std::chrono::system_clock::duration>>& status) const
{ {
auto now = clock.now(); auto now = clock.now();
@ -206,8 +284,8 @@ namespace libcron
}); });
} }
template<typename ClockType> template<typename ClockType, typename LockType>
std::ostream& operator<<(std::ostream& stream, const Cron<ClockType>& c) std::ostream& operator<<(std::ostream& stream, const Cron<ClockType, LockType>& c)
{ {
std::for_each(c.tasks.get_tasks().cbegin(), c.tasks.get_tasks().cend(), std::for_each(c.tasks.get_tasks().cbegin(), c.tasks.get_tasks().cend(),
[&stream, &c](const Task& t) [&stream, &c](const Task& t)

View File

@ -16,7 +16,7 @@ namespace libcron
static CronData create(const std::string& cron_expression); static CronData create(const std::string& cron_expression);
CronData(); CronData() = default;
CronData(const CronData&) = default; CronData(const CronData&) = default;
@ -77,6 +77,9 @@ namespace libcron
template<typename T> template<typename T>
bool convert_from_string_range_to_number_range(const std::string& range, std::set<T>& numbers); bool convert_from_string_range_to_number_range(const std::string& range, std::set<T>& numbers);
template<typename T>
static std::string& replace_string_name_with_numeric(std::string& s);
private: private:
void parse(const std::string& cron_expression); void parse(const std::string& cron_expression);
@ -121,8 +124,8 @@ namespace libcron
std::set<DayOfWeek> day_of_week{}; std::set<DayOfWeek> day_of_week{};
bool valid = false; bool valid = false;
std::vector<std::string> month_names; static const std::vector<std::string> month_names;
std::vector<std::string> day_names; static const std::vector<std::string> day_names;
template<typename T> template<typename T>
void add_full_range(std::set<T>& set); void add_full_range(std::set<T>& set);
@ -343,4 +346,40 @@ namespace libcron
return res; return res;
} }
template<typename T>
std::string & CronData::replace_string_name_with_numeric(std::string& s)
{
auto value = static_cast<int>(T::First);
const std::vector<std::string>* name_source{};
static_assert(std::is_same<T, libcron::Months>()
|| std::is_same<T, libcron::DayOfWeek>(),
"T must be either Months or DayOfWeek");
if constexpr (std::is_same<T, libcron::Months>())
{
name_source = &month_names;
}
else
{
name_source = &day_names;
}
for (const auto& name : *name_source)
{
std::regex m(name, std::regex_constants::ECMAScript | std::regex_constants::icase);
std::string replaced;
std::regex_replace(std::back_inserter(replaced), s.begin(), s.end(), m, std::to_string(value));
s = replaced;
++value;
}
return s;
}
} }

View File

@ -44,7 +44,7 @@ namespace libcron
std::smatch random_match; std::smatch random_match;
if (std::regex_match(section.begin(), section.end(), random_match, rand_expression)) if (std::regex_match(section.cbegin(), section.cend(), random_match, rand_expression))
{ {
// Random range, get left and right numbers. // Random range, get left and right numbers.
auto left = std::stoi(random_match[1].str()); auto left = std::stoi(random_match[1].str());

View File

@ -1,26 +1,42 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include "CronData.h"
#include "CronSchedule.h"
#include <chrono> #include <chrono>
#include <utility> #include <utility>
#include "CronData.h"
#include "CronSchedule.h"
namespace libcron namespace libcron
{ {
class Task class TaskInformation
{ {
public: public:
virtual ~TaskInformation() = default;
virtual std::chrono::system_clock::duration get_delay() const = 0;
};
Task(std::string name, const CronSchedule schedule, std::function<void()> task) class Task : public TaskInformation
{
public:
using TaskFunction = std::function<void(const TaskInformation&)>;
Task(std::string name, const CronSchedule schedule, TaskFunction task)
: name(std::move(name)), schedule(std::move(schedule)), task(std::move(task)) : name(std::move(name)), schedule(std::move(schedule)), task(std::move(task))
{ {
} }
void execute(std::chrono::system_clock::time_point now) void execute(std::chrono::system_clock::time_point now)
{ {
// Next Schedule is still the current schedule, calculate delay (actual execution - planned execution)
delay = now - next_schedule;
last_run = now; last_run = now;
task(); task(*this);
}
std::chrono::system_clock::duration get_delay() const override
{
return delay;
} }
Task(const Task& other) = default; Task(const Task& other) = default;
@ -50,8 +66,29 @@ namespace libcron
std::string name; std::string name;
CronSchedule schedule; CronSchedule schedule;
std::chrono::system_clock::time_point next_schedule; std::chrono::system_clock::time_point next_schedule;
std::function<void()> task; std::chrono::system_clock::duration delay = std::chrono::seconds(-1);
TaskFunction task;
bool valid = false; bool valid = false;
std::chrono::system_clock::time_point last_run = std::numeric_limits<std::chrono::system_clock::time_point>::min(); std::chrono::system_clock::time_point last_run = std::numeric_limits<std::chrono::system_clock::time_point>::min();
}; };
} }
inline bool operator==(const std::string &lhs, const libcron::Task &rhs)
{
return lhs == rhs.get_name();
}
inline bool operator==(const libcron::Task &lhs, const std::string &rhs)
{
return lhs.get_name() == rhs;
}
inline bool operator!=(const std::string &lhs, const libcron::Task &rhs)
{
return !(lhs == rhs);
}
inline bool operator!=(const libcron::Task &lhs, const std::string &rhs)
{
return !(lhs == rhs);
}

View File

@ -13,6 +13,9 @@ namespace libcron
Months::October, Months::October,
Months::December }; Months::December };
const std::vector<std::string> CronData::month_names{ "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
const std::vector<std::string> CronData::day_names{ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
CronData CronData::create(const std::string& cron_expression) CronData CronData::create(const std::string& cron_expression)
{ {
CronData c; CronData c;
@ -21,12 +24,6 @@ namespace libcron
return c; return c;
} }
CronData::CronData()
: month_names({ "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }),
day_names({ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" })
{
}
void CronData::parse(const std::string& cron_expression) void CronData::parse(const std::string& cron_expression)
{ {
// First, split on white-space. We expect six parts. // First, split on white-space. We expect six parts.

View File

@ -18,14 +18,46 @@ namespace libcron
std::tuple<bool, std::string> CronRandomization::parse(const std::string& cron_schedule) std::tuple<bool, std::string> CronRandomization::parse(const std::string& cron_schedule)
{ {
// Split on space to get each separate part, six parts expected // Split on space to get each separate part, six parts expected
std::regex split{ R"#(^\s*(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$)#", const std::regex split{ R"#(^\s*(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$)#",
std::regex_constants::ECMAScript }; std::regex_constants::ECMAScript };
std::smatch all_sections; std::smatch all_sections;
auto res = std::regex_match(cron_schedule.cbegin(), cron_schedule.cend(), all_sections, split);
std::string final_cron_schedule; // Replace text with numbers
std::string working_copy{};
auto res = std::regex_match(cron_schedule.begin(), cron_schedule.end(), all_sections, split); if (res)
{
// Merge seconds, minutes, hours and day of month back together
working_copy += all_sections[1].str();
working_copy += " ";
working_copy += all_sections[2].str();
working_copy += " ";
working_copy += all_sections[3].str();
working_copy += " ";
working_copy += all_sections[4].str();
working_copy += " ";
// Replace month names
auto month = all_sections[5].str();
CronData::replace_string_name_with_numeric<libcron::Months>(month);
working_copy += " ";
working_copy += month;
// Replace day names
auto dow = all_sections[6].str();
CronData::replace_string_name_with_numeric<libcron::DayOfWeek>(dow);
working_copy += " ";
working_copy += dow;
}
std::string final_cron_schedule{};
// Split again on space
res = res && std::regex_match(working_copy.cbegin(), working_copy.cend(), all_sections, split);
if (res) if (res)
{ {

View File

@ -46,7 +46,7 @@ namespace libcron
//Add days until the current weekday is one of the allowed weekdays //Add days until the current weekday is one of the allowed weekdays
year_month_weekday ymw = date::floor<days>(curr); year_month_weekday ymw = date::floor<days>(curr);
if (data.get_day_of_week().find(static_cast<DayOfWeek>(unsigned(ymw.weekday()))) == if (data.get_day_of_week().find(static_cast<DayOfWeek>(ymw.weekday().c_encoding())) ==
data.get_day_of_week().end()) data.get_day_of_week().end())
{ {
sys_days s = ymd; sys_days s = ymd;

View File

@ -1,4 +1,3 @@
#include <iostream>
#include "libcron/Task.h" #include "libcron/Task.h"
using namespace std::chrono; using namespace std::chrono;

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.6) cmake_minimum_required(VERSION 3.6)
project(cron_test) project(cron_test)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
if( MSVC ) if( MSVC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")

View File

@ -225,3 +225,16 @@ SCENARIO("Date that exist in one of the months")
{ {
REQUIRE(CronData::create("0 0 * 31 APR,MAY ?").is_valid()); REQUIRE(CronData::create("0 0 * 31 APR,MAY ?").is_valid());
} }
SCENARIO("Replacing text with numbers")
{
{
std::string s = "SUN-TUE";
REQUIRE(CronData::replace_string_name_with_numeric<libcron::DayOfWeek>(s) == "0-2");
}
{
std::string s = "JAN-DEC";
REQUIRE(CronData::replace_string_name_with_numeric<libcron::Months>(s) == "1-12");
}
}

View File

@ -7,21 +7,31 @@
#include <iostream> #include <iostream>
using namespace libcron; using namespace libcron;
const auto EXPECT_FAILURE = true;
void test(const char* const random_schedule) void test(const char* const random_schedule, bool expect_failure = false)
{ {
libcron::CronRandomization cr; libcron::CronRandomization cr;
std::unordered_map<int, std::unordered_map<int, int>> results{};
for (int i = 0; i < 5000; ++i) for (int i = 0; i < 5000; ++i)
{ {
auto res = cr.parse(random_schedule); auto res = cr.parse(random_schedule);
REQUIRE(std::get<0>(res));
auto schedule = std::get<1>(res); auto schedule = std::get<1>(res);
INFO("schedule:" << schedule);
Cron<> cron; Cron<> cron;
REQUIRE(cron.add_schedule("validate schedule", schedule, []() {}));
if(expect_failure)
{
// Parsing of random might succeed, but it yields an invalid schedule.
auto r = std::get<0>(res) && cron.add_schedule("validate schedule", schedule, [](auto&) {});
REQUIRE_FALSE(r);
}
else
{
REQUIRE(std::get<0>(res));
REQUIRE(cron.add_schedule("validate schedule", schedule, [](auto&) {}));
}
} }
} }
@ -103,3 +113,68 @@ SCENARIO("Test readme examples")
} }
} }
} }
SCENARIO("Randomization using text versions of days and months")
{
GIVEN("0 0 0 ? * R(TUE-FRI)")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? * R(TUE-FRI)");
}
}
GIVEN("Valid schedule")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(JAN-DEC) R(MON-FRI)");
}
AND_WHEN("Given 0 0 0 ? R(DEC-MAR) R(SAT-SUN)")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(DEC-MAR) R(SAT-SUN)");
}
}
AND_THEN("Given 0 0 0 ? R(JAN-FEB) *")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(JAN-FEB) *");
}
}
AND_THEN("Given 0 0 0 ? R(OCT-OCT) *")
{
THEN("Valid schedule generated")
{
test("0 0 0 ? R(OCT-OCT) *");
}
}
}
GIVEN("Invalid schedule")
{
THEN("No schedule generated")
{
// Day of month specified - not allowed with day of week
test("0 0 0 1 R(JAN-DEC) R(MON-SUN)", EXPECT_FAILURE);
}
AND_THEN("No schedule generated")
{
// Invalid range
test("0 0 0 ? R(JAN) *", EXPECT_FAILURE);
}
AND_THEN("No schedule generated")
{
// Days in month field
test("0 0 0 ? R(MON-TUE) *", EXPECT_FAILURE);
}
AND_THEN("No schedule generated")
{
// Month in day field
test("0 0 0 ? * R(JAN-JUN)", EXPECT_FAILURE);
}
}
}

View File

@ -35,7 +35,7 @@ SCENARIO("Adding a task")
WHEN("Adding a task that runs every second") WHEN("Adding a task that runs every second")
{ {
REQUIRE(c.add_schedule("A task", "* * * * * ?", REQUIRE(c.add_schedule("A task", "* * * * * ?",
[&expired]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })
@ -68,7 +68,7 @@ SCENARIO("Adding a task that expires in the future")
Cron<> c; Cron<> c;
REQUIRE(c.add_schedule("A task", REQUIRE(c.add_schedule("A task",
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}), create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}),
[&expired]() [&expired](auto&)
{ {
expired = true; expired = true;
}) })
@ -99,6 +99,50 @@ SCENARIO("Adding a task that expires in the future")
} }
} }
SCENARIO("Get delay using Task-Information")
{
using namespace std::chrono_literals;
GIVEN("A Cron instance with one task expiring in 2 seconds, but taking 3 seconds to execute")
{
auto _2_second_expired = 0;
auto _delay = std::chrono::system_clock::duration(-1s);
Cron<> c;
REQUIRE(c.add_schedule("Two",
"*/2 * * * * ?",
[&_2_second_expired, &_delay](auto& i)
{
_2_second_expired++;
_delay = i.get_delay();
std::this_thread::sleep_for(3s);
})
);
THEN("Not yet expired")
{
REQUIRE_FALSE(_2_second_expired);
REQUIRE(_delay <= 0s);
}
WHEN("Exactly schedule task")
{
while (_2_second_expired == 0)
c.tick();
THEN("Task should have expired within a valid time")
{
REQUIRE(_2_second_expired == 1);
REQUIRE(_delay <= 1s);
}
AND_THEN("Executing another tick again, leading to execute task again immediatly, but not on time as execution has taken 3 seconds.")
{
c.tick();
REQUIRE(_2_second_expired == 2);
REQUIRE(_delay >= 1s);
}
}
}
}
SCENARIO("Task priority") SCENARIO("Task priority")
{ {
GIVEN("A Cron instance with two tasks expiring in 3 and 5 seconds, added in 'reverse' order") GIVEN("A Cron instance with two tasks expiring in 3 and 5 seconds, added in 'reverse' order")
@ -110,7 +154,7 @@ SCENARIO("Task priority")
Cron<> c; Cron<> c;
REQUIRE(c.add_schedule("Five", REQUIRE(c.add_schedule("Five",
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{5}), create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{5}),
[&_5_second_expired]() [&_5_second_expired](auto&)
{ {
_5_second_expired++; _5_second_expired++;
}) })
@ -118,7 +162,7 @@ SCENARIO("Task priority")
REQUIRE(c.add_schedule("Three", REQUIRE(c.add_schedule("Three",
create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}), create_schedule_expiring_in(c.get_clock().now(), hours{0}, minutes{0}, seconds{3}),
[&_3_second_expired]() [&_3_second_expired](auto&)
{ {
_3_second_expired++; _3_second_expired++;
}) })
@ -231,7 +275,7 @@ SCENARIO("Clock changes")
clock.set(sys_days{2018_y / 05 / 05}); clock.set(sys_days{2018_y / 05 / 05});
// Every hour // Every hour
REQUIRE(c.add_schedule("Clock change task", "0 0 * * * ?", []() REQUIRE(c.add_schedule("Clock change task", "0 0 * * * ?", [](auto&)
{ {
}) })
); );
@ -309,7 +353,7 @@ SCENARIO("Multiple ticks per second")
int run_count = 0; int run_count = 0;
// Every 10 seconds // Every 10 seconds
REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count]() REQUIRE(c.add_schedule("Clock change task", "*/10 0 * * * ?", [&run_count](auto&)
{ {
run_count++; run_count++;
}) })
@ -335,3 +379,70 @@ SCENARIO("Multiple ticks per second")
} }
} }
SCENARIO("Tasks can be added and removed from the scheduler")
{
GIVEN("A Cron instance with no task")
{
Cron<> c;
auto expired = false;
WHEN("Adding 5 tasks that runs every second")
{
REQUIRE(c.add_schedule("Task-1", "* * * * * ?",
[&expired](auto&)
{
expired = true;
})
);
REQUIRE(c.add_schedule("Task-2", "* * * * * ?",
[&expired](auto&)
{
expired = true;
})
);
REQUIRE(c.add_schedule("Task-3", "* * * * * ?",
[&expired](auto&)
{
expired = true;
})
);
REQUIRE(c.add_schedule("Task-4", "* * * * * ?",
[&expired](auto&)
{
expired = true;
})
);
REQUIRE(c.add_schedule("Task-5", "* * * * * ?",
[&expired](auto&)
{
expired = true;
})
);
THEN("Count is 5")
{
REQUIRE(c.count() == 5);
}
AND_THEN("Removing all scheduled tasks")
{
c.clear_schedules();
REQUIRE(c.count() == 0);
}
AND_THEN("Removing a task that does not exist")
{
c.remove_schedule("Task-6");
REQUIRE(c.count() == 5);
}
AND_THEN("Removing a task that does exist")
{
c.remove_schedule("Task-5");
REQUIRE(c.count() == 4);
}
}
}
}