mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-04-22 08:23:04 -05:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
aa3d4368d5 | ||
|
7c7d290792 | ||
|
41f238ceb0 | ||
|
0dd9df49d7 | ||
|
5f8ecc9690 | ||
|
5c8de082c1 | ||
|
a3b892a24a | ||
|
d4679b7c3c | ||
|
e91a51afc1 | ||
|
b0046755bd | ||
|
9edb758ca8 | ||
|
f3fddf5f19 |
21
.github/workflows/ci.yml
vendored
Normal file
21
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
name: libcron tests
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
Tests:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: true
|
||||
- name: Build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
make -j4
|
||||
- name: Test
|
||||
run: |
|
||||
cd test/out
|
||||
./cron_test
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
||||
[submodule "libcron/externals/date"]
|
||||
path = libcron/externals/date
|
||||
url = https://github.com/HowardHinnant/date.git
|
||||
[submodule "test/externals/Catch2"]
|
||||
path = test/externals/Catch2
|
||||
url = https://github.com/catchorg/Catch2.git
|
||||
|
@ -6,3 +6,7 @@ add_subdirectory(test)
|
||||
|
||||
add_dependencies(cron_test libcron)
|
||||
|
||||
install(TARGETS libcron DESTINATION lib)
|
||||
install(DIRECTORY libcron/include/libcron DESTINATION include)
|
||||
install(DIRECTORY libcron/externals/date/include/date DESTINATION include)
|
||||
|
||||
|
61
README.md
61
README.md
@ -23,6 +23,8 @@ while(true)
|
||||
}
|
||||
```
|
||||
|
||||
In case there is a lot of time between you call `add_schedule` and `tick`, you can call `recalculate_schedule`.
|
||||
|
||||
The callback must have the following signature:
|
||||
|
||||
```
|
||||
@ -45,6 +47,46 @@ cron.add_schedule("Hello from Cron", "* * * * * ?", [=](auto& i) {
|
||||
});
|
||||
```
|
||||
|
||||
- `libcron::TaskInformation::get_name` gives you the name of the current Task. This allows to add attach the same callback to multiple schedules:
|
||||
|
||||
```
|
||||
libcron::Cron cron;
|
||||
|
||||
auto f = [](auto& i) {
|
||||
if (i.get_name() == "Task 1")
|
||||
{
|
||||
do_work_task_1();
|
||||
}
|
||||
else if (i.get_name() == "Task 2")
|
||||
{
|
||||
do_work_task_2();
|
||||
}
|
||||
};
|
||||
|
||||
cron.add_schedule("Task 1", "* * * * * ?", f);
|
||||
cron.add_schedule("Task 2", "* * * * * ?", f);
|
||||
```
|
||||
|
||||
## Adding multiple tasks with individual schedules at once
|
||||
|
||||
libcron::cron::add_schedule needs to sort the underlying container each time you add a schedule. To improve performance when adding many tasks by only sorting once, there is a convinient way to pass either a `std::map<std::string, std::string>`, a `std::vector<std::pair<std::string, std::string>>`, a `std::vector<std::tuple<std::string, std::string>>` or a `std::unordered_map<std::string, std::string>` to `add_schedule`, where the first element corresponds to the task name and the second element to the task schedule. Only if all schedules in the container are valid, they will be added to `libcron::Cron`. The return type is a `std::tuple<bool, std::string, std::string>`, where the boolean is `true` if the schedules have been added or false otherwise. If the schedules have not been added, the second element in the tuple corresponds to the task-name with the given invalid schedule. If there are multiple invalid schedules in the container, `add_schedule` will abort at the first invalid element:
|
||||
|
||||
```
|
||||
std::map<std::string, std::string> name_schedule_map;
|
||||
for(int i = 1; i <= 1000; i++)
|
||||
{
|
||||
name_schedule_map["Task-" + std::to_string(i)] = "* * * * * ?";
|
||||
}
|
||||
name_schedule_map["Task-1000"] = "invalid";
|
||||
auto res = c1.add_schedule(name_schedule_map, [](auto&) { });
|
||||
if (std::get<0>(res) == false)
|
||||
{
|
||||
std::cout << "Task " << std::get<1>(res)
|
||||
<< "has an invalid schedule: "
|
||||
<< std::get<2>(res) << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Removing schedules from `libcron::Cron`
|
||||
@ -146,10 +188,29 @@ the '?'-character to ensure that it is not possible to specify a statement which
|
||||
|Expression | Meaning
|
||||
| --- | --- |
|
||||
| * * * * * ? | Every second
|
||||
| 0 * * * * ? | Every minute
|
||||
| 0 0 12 * * MON-FRI | Every Weekday at noon
|
||||
| 0 0 12 1/2 * ? | Every 2 days, starting on the 1st at noon
|
||||
| 0 0 */12 ? * * | Every twelve hours
|
||||
| @hourly | Every hour
|
||||
|
||||
Note that the expression formatting has a part for seconds and the day of week.
|
||||
For the day of week part, a question mark ? is utilized. This format
|
||||
may not be parsed by all online crontab calculators or expression generators.
|
||||
|
||||
## Convenience scheduling
|
||||
|
||||
These special time specification tokens which replace the 5 initial time and date fields, and are prefixed with the '@' character, are supported:
|
||||
|
||||
|Token|Meaning
|
||||
| --- | --- |
|
||||
| @yearly | Run once a year, ie. "0 0 1 1 *".
|
||||
| @annually | Run once a year, ie. "0 0 1 1 *".
|
||||
| @monthly | Run once a month, ie. "0 0 1 * *".
|
||||
| @weekly | Run once a week, ie. "0 0 * * 0".
|
||||
| @daily | Run once a day, ie. "0 0 * * *".
|
||||
| @hourly | Run once an hour, ie. "0 * * * *".
|
||||
|
||||
# Randomization
|
||||
|
||||
The standard cron format does not allow for randomization, but with the use of `CronRandomization` you can generate random
|
||||
|
@ -3,8 +3,15 @@ project(libcron)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Deactivate Iterator-Debugging on Windows
|
||||
option(LIBCRON_DEACTIVATE_ITERATOR_DEBUGGING "Build with iterator-debugging (MSVC only)." OFF)
|
||||
|
||||
if( MSVC )
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||
|
||||
if (LIBCRON_DEACTIVATE_ITERATOR_DEBUGGING)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_HAS_ITERATOR_DEBUGGING=0")
|
||||
endif()
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
|
||||
endif()
|
||||
@ -28,6 +35,11 @@ target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/externals/date/include
|
||||
PUBLIC include)
|
||||
|
||||
if(NOT MSVC)
|
||||
# Assume a modern compiler (gcc 9.3)
|
||||
target_compile_definitions (${PROJECT_NAME} PRIVATE -DHAS_UNCAUGHT_EXCEPTIONS)
|
||||
endif()
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
|
@ -2,11 +2,14 @@
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "Task.h"
|
||||
#include "CronClock.h"
|
||||
#include "TaskQueue.h"
|
||||
|
||||
namespace libcron
|
||||
{
|
||||
@ -32,11 +35,16 @@ namespace libcron
|
||||
template<typename ClockType, typename LockType>
|
||||
std::ostream& operator<<(std::ostream& stream, const Cron<ClockType, LockType>& c);
|
||||
|
||||
template<typename ClockType = libcron::LocalClock, typename LockType = libcron::NullLock>
|
||||
template<typename ClockType = libcron::LocalClock,
|
||||
typename LockType = libcron::NullLock>
|
||||
class Cron
|
||||
{
|
||||
public:
|
||||
bool add_schedule(std::string name, const std::string& schedule, Task::TaskFunction work);
|
||||
|
||||
template<typename Schedules = std::map<std::string, std::string>>
|
||||
std::tuple<bool, std::string, std::string>
|
||||
add_schedule(const Schedules& name_schedule_map, Task::TaskFunction work);
|
||||
void clear_schedules();
|
||||
void remove_schedule(const std::string& name);
|
||||
|
||||
@ -63,75 +71,23 @@ namespace libcron
|
||||
return clock;
|
||||
}
|
||||
|
||||
void recalculate_schedule()
|
||||
{
|
||||
for (auto& t : tasks.get_tasks())
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
// Ensure that next schedule is in the future
|
||||
t.calculate_next(clock.now() + 1s);
|
||||
}
|
||||
}
|
||||
|
||||
void get_time_until_expiry_for_tasks(
|
||||
std::vector<std::tuple<std::string, std::chrono::system_clock::duration>>& status) const;
|
||||
|
||||
friend std::ostream& operator<<<>(std::ostream& stream, const Cron<ClockType, LockType>& c);
|
||||
|
||||
private:
|
||||
class Queue
|
||||
// Priority queue placing smallest (i.e. nearest in time) items on top.
|
||||
: public std::priority_queue<Task, std::vector<Task>, std::greater<>>
|
||||
{
|
||||
public:
|
||||
// Inherit to allow access to the container.
|
||||
const std::vector<Task>& get_tasks() const
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
std::vector<Task>& get_tasks()
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
Queue tasks{};
|
||||
TaskQueue<LockType> tasks{};
|
||||
ClockType clock{};
|
||||
bool first_tick = true;
|
||||
std::chrono::system_clock::time_point last_tick{};
|
||||
@ -149,6 +105,7 @@ namespace libcron
|
||||
if (t.calculate_next(clock.now()))
|
||||
{
|
||||
tasks.push(t);
|
||||
tasks.sort();
|
||||
}
|
||||
tasks.release_queue();
|
||||
}
|
||||
@ -156,6 +113,50 @@ namespace libcron
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename ClockType, typename LockType>
|
||||
template<typename Schedules>
|
||||
std::tuple<bool, std::string, std::string>
|
||||
Cron<ClockType, LockType>::add_schedule(const Schedules& name_schedule_map, Task::TaskFunction work)
|
||||
{
|
||||
bool is_valid = true;
|
||||
std::tuple<bool, std::string, std::string> res{false, "", ""};
|
||||
|
||||
std::vector<Task> tasks_to_add;
|
||||
tasks_to_add.reserve(name_schedule_map.size());
|
||||
|
||||
for (auto it = name_schedule_map.begin(); is_valid && it != name_schedule_map.end(); ++it)
|
||||
{
|
||||
const auto& [name, schedule] = *it;
|
||||
auto cron = CronData::create(schedule);
|
||||
is_valid = cron.is_valid();
|
||||
if (is_valid)
|
||||
{
|
||||
Task t{std::move(name), CronSchedule{cron}, work };
|
||||
if (t.calculate_next(clock.now()))
|
||||
{
|
||||
tasks_to_add.push_back(std::move(t));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::get<1>(res) = name;
|
||||
std::get<2>(res) = schedule;
|
||||
}
|
||||
}
|
||||
|
||||
// Only add tasks and sort once if all elements in the map where valid
|
||||
if (is_valid && tasks_to_add.size() > 0)
|
||||
{
|
||||
tasks.lock_queue();
|
||||
tasks.push(tasks_to_add);
|
||||
tasks.sort();
|
||||
tasks.release_queue();
|
||||
}
|
||||
|
||||
std::get<0>(res) = is_valid;
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename ClockType, typename LockType>
|
||||
void Cron<ClockType, LockType>::clear_schedules()
|
||||
{
|
||||
@ -241,30 +242,31 @@ namespace libcron
|
||||
|
||||
last_tick = now;
|
||||
|
||||
std::vector<Task> executed{};
|
||||
|
||||
while (!tasks.empty()
|
||||
&& tasks.top().is_expired(now))
|
||||
if (!tasks.empty())
|
||||
{
|
||||
executed.push_back(tasks.top());
|
||||
tasks.pop();
|
||||
auto& t = executed[executed.size() - 1];
|
||||
t.execute(now);
|
||||
}
|
||||
|
||||
res = executed.size();
|
||||
|
||||
// Place executed tasks back onto the priority queue.
|
||||
std::for_each(executed.begin(), executed.end(), [this, &now](Task& task)
|
||||
{
|
||||
// Must calculate new schedules using second after 'now', otherwise
|
||||
// we'll run the same task over and over if it takes less than 1s to execute.
|
||||
using namespace std::chrono_literals;
|
||||
if (task.calculate_next(now + 1s))
|
||||
for (size_t i = 0; i < tasks.size(); i++)
|
||||
{
|
||||
tasks.push(task);
|
||||
if (tasks.at(i).is_expired(now))
|
||||
{
|
||||
auto& t = tasks.at(i);
|
||||
t.execute(now);
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
if (!t.calculate_next(now + 1s))
|
||||
{
|
||||
tasks.remove(t);
|
||||
}
|
||||
|
||||
res++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Only sort if at least one task was executed
|
||||
if (res > 0)
|
||||
{
|
||||
tasks.sort();
|
||||
}
|
||||
}
|
||||
|
||||
tasks.release_queue();
|
||||
return res;
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <libcron/TimeTypes.h>
|
||||
|
||||
namespace libcron
|
||||
@ -20,6 +21,8 @@ namespace libcron
|
||||
|
||||
CronData(const CronData&) = default;
|
||||
|
||||
CronData& operator=(const CronData&) = default;
|
||||
|
||||
bool is_valid() const
|
||||
{
|
||||
return valid;
|
||||
@ -126,6 +129,7 @@ namespace libcron
|
||||
|
||||
static const std::vector<std::string> month_names;
|
||||
static const std::vector<std::string> day_names;
|
||||
static std::unordered_map<std::string, CronData> cache;
|
||||
|
||||
template<typename T>
|
||||
void add_full_range(std::set<T>& set);
|
||||
|
@ -2,12 +2,12 @@
|
||||
|
||||
#include "libcron/CronData.h"
|
||||
#include <chrono>
|
||||
#ifdef _WIN32
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable:4244)
|
||||
#endif
|
||||
#include <date/date.h>
|
||||
#ifdef _WIN32
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
@ -25,6 +25,8 @@ namespace libcron
|
||||
|
||||
CronSchedule(const CronSchedule&) = default;
|
||||
|
||||
CronSchedule& operator=(const CronSchedule&) = default;
|
||||
|
||||
std::tuple<bool, std::chrono::system_clock::time_point>
|
||||
calculate_from(const std::chrono::system_clock::time_point& from) const;
|
||||
|
||||
@ -51,4 +53,4 @@ namespace libcron
|
||||
CronData data;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace libcron
|
||||
public:
|
||||
virtual ~TaskInformation() = default;
|
||||
virtual std::chrono::system_clock::duration get_delay() const = 0;
|
||||
virtual std::string get_name() const = 0;
|
||||
};
|
||||
|
||||
class Task : public TaskInformation
|
||||
@ -50,12 +51,17 @@ namespace libcron
|
||||
return next_schedule > other.next_schedule;
|
||||
}
|
||||
|
||||
bool operator<(const Task& other) const
|
||||
{
|
||||
return next_schedule < other.next_schedule;
|
||||
}
|
||||
|
||||
bool is_expired(std::chrono::system_clock::time_point now) const;
|
||||
|
||||
std::chrono::system_clock::duration
|
||||
time_until_expiry(std::chrono::system_clock::time_point now) const;
|
||||
|
||||
std::string get_name() const
|
||||
std::string get_name() const override
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
115
libcron/include/libcron/TaskQueue.h
Normal file
115
libcron/include/libcron/TaskQueue.h
Normal file
@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "Task.h"
|
||||
|
||||
namespace libcron
|
||||
{
|
||||
template<typename LockType>
|
||||
class TaskQueue
|
||||
{
|
||||
public:
|
||||
const std::vector<Task>& get_tasks() const
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
std::vector<Task>& get_tasks()
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
size_t size() const noexcept
|
||||
{
|
||||
return c.size();
|
||||
}
|
||||
|
||||
bool empty() const noexcept
|
||||
{
|
||||
return c.empty();
|
||||
}
|
||||
|
||||
void push(Task& t)
|
||||
{
|
||||
c.push_back(std::move(t));
|
||||
}
|
||||
|
||||
void push(Task&& t)
|
||||
{
|
||||
c.push_back(std::move(t));
|
||||
}
|
||||
|
||||
void push(std::vector<Task>& tasks_to_insert)
|
||||
{
|
||||
c.reserve(c.size() + tasks_to_insert.size());
|
||||
c.insert(c.end(), std::make_move_iterator(tasks_to_insert.begin()), std::make_move_iterator(tasks_to_insert.end()));
|
||||
}
|
||||
|
||||
const Task& top() const
|
||||
{
|
||||
return c[0];
|
||||
}
|
||||
|
||||
Task& at(const size_t i)
|
||||
{
|
||||
return c[i];
|
||||
}
|
||||
|
||||
void sort()
|
||||
{
|
||||
std::sort(c.begin(), c.end(), std::less<>());
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
lock.lock();
|
||||
c.clear();
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
void remove(Task& to_remove)
|
||||
{
|
||||
auto it = std::find_if(c.begin(), c.end(), [&to_remove] (const Task& to_compare) {
|
||||
return to_remove.get_name() == to_compare;
|
||||
});
|
||||
|
||||
if (it != c.end())
|
||||
{
|
||||
c.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void remove(std::string to_remove)
|
||||
{
|
||||
lock.lock();
|
||||
auto it = std::find_if(c.begin(), c.end(), [&to_remove] (const Task& to_compare) {
|
||||
return to_remove == to_compare;
|
||||
});
|
||||
if (it != c.end())
|
||||
{
|
||||
c.erase(it);
|
||||
}
|
||||
|
||||
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;
|
||||
std::vector<Task> c;
|
||||
};
|
||||
}
|
@ -15,24 +15,44 @@ namespace libcron
|
||||
|
||||
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" };
|
||||
std::unordered_map<std::string, CronData> CronData::cache{};
|
||||
|
||||
CronData CronData::create(const std::string& cron_expression)
|
||||
{
|
||||
CronData c;
|
||||
c.parse(cron_expression);
|
||||
auto found = cache.find(cron_expression);
|
||||
|
||||
if (found == cache.end())
|
||||
{
|
||||
c.parse(cron_expression);
|
||||
cache[cron_expression] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
c = found->second;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void CronData::parse(const std::string& cron_expression)
|
||||
{
|
||||
// First, split on white-space. We expect six parts.
|
||||
// First, check for "convenience scheduling" using @yearly, @annually,
|
||||
// @monthly, @weekly, @daily or @hourly.
|
||||
std::string tmp = std::regex_replace(cron_expression, std::regex("@yearly"), "0 0 1 1 *");
|
||||
tmp = std::regex_replace(tmp, std::regex("@annually"), "0 0 1 1 *");
|
||||
tmp = std::regex_replace(tmp, std::regex("@monthly"), "0 0 1 * *");
|
||||
tmp = std::regex_replace(tmp, std::regex("@weekly"), "0 0 * * 0");
|
||||
tmp = std::regex_replace(tmp, std::regex("@daily"), "0 0 * * *");
|
||||
const std::string expression = std::regex_replace(tmp, std::regex("@hourly"), "0 * * * *");
|
||||
|
||||
// Second, split on white-space. We expect six parts.
|
||||
std::regex split{ R"#(^\s*(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$)#",
|
||||
std::regex_constants::ECMAScript };
|
||||
|
||||
std::smatch match;
|
||||
|
||||
if (std::regex_match(cron_expression.begin(), cron_expression.end(), match, split))
|
||||
if (std::regex_match(expression.begin(), expression.end(), match, split))
|
||||
{
|
||||
valid = validate_numeric<Seconds>(match[1], seconds);
|
||||
valid &= validate_numeric<Minutes>(match[2], minutes);
|
||||
|
@ -81,6 +81,16 @@ namespace libcron
|
||||
}
|
||||
}
|
||||
|
||||
// Discard fraction seconds in the calculated schedule time
|
||||
// that may leftover from the argument `from`, which in turn comes from `now()`.
|
||||
// Fraction seconds will potentially make the task be triggered more than 1 second late
|
||||
// if the `tick()` within the same second is earlier than schedule time,
|
||||
// in that the task will not trigger until the next `tick()` next second.
|
||||
// By discarding fraction seconds in the scheduled time,
|
||||
// the `tick()` within the same second will never be earlier than schedule time,
|
||||
// and the task will trigger in that `tick()`.
|
||||
curr -= curr.time_since_epoch() % seconds{1};
|
||||
|
||||
return std::make_tuple(max_iterations > 0, curr);
|
||||
}
|
||||
}
|
@ -3,14 +3,21 @@ project(cron_test)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Deactivate Iterator-Debugging on Windows
|
||||
option(LIBCRON_DEACTIVATE_ITERATOR_DEBUGGING "Build with iterator-debugging (MSVC only)." OFF)
|
||||
|
||||
if( MSVC )
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
||||
|
||||
if (LIBCRON_DEACTIVATE_ITERATOR_DEBUGGING)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_HAS_ITERATOR_DEBUGGING=0")
|
||||
endif()
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
|
||||
endif()
|
||||
|
||||
include_directories(
|
||||
${CMAKE_CURRENT_LIST_DIR}/externals/Catch2/single_include/
|
||||
${CMAKE_CURRENT_LIST_DIR}/externals/Catch2/single_include/catch2
|
||||
${CMAKE_CURRENT_LIST_DIR}/../libcron/externals/date/include
|
||||
${CMAKE_CURRENT_LIST_DIR}/..
|
||||
)
|
||||
@ -19,12 +26,19 @@ add_executable(
|
||||
${PROJECT_NAME}
|
||||
CronDataTest.cpp
|
||||
CronRandomizationTest.cpp
|
||||
CronScheduleTest.cpp
|
||||
CronTest.cpp)
|
||||
CronScheduleTest.cpp
|
||||
CronTest.cpp)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} libcron)
|
||||
if(NOT MSVC)
|
||||
target_link_libraries(${PROJECT_NAME} libcron pthread)
|
||||
|
||||
# Assume a modern compiler supporting uncaught_exceptions()
|
||||
target_compile_definitions (${PROJECT_NAME} PRIVATE -DHAS_UNCAUGHT_EXCEPTIONS)
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} libcron)
|
||||
endif()
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out")
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/out")
|
||||
|
@ -184,6 +184,15 @@ SCENARIO("Examples from README.md")
|
||||
DT(2018_y / 03 / 1, hours{12}, minutes{13}, seconds{48})
|
||||
}));
|
||||
|
||||
REQUIRE(test("0 * * * * ?", DT(2018_y / 03 / 1, hours{ 12 }, minutes{ 0 }, seconds{ 10 }),
|
||||
{
|
||||
DT(2018_y / 03 / 1, hours{12}, minutes{1}, seconds{0}),
|
||||
DT(2018_y / 03 / 1, hours{12}, minutes{2}, seconds{0}),
|
||||
DT(2018_y / 03 / 1, hours{12}, minutes{3}, seconds{0}),
|
||||
DT(2018_y / 03 / 1, hours{12}, minutes{4}, seconds{0})
|
||||
}));
|
||||
|
||||
|
||||
REQUIRE(test("0 0 12 * * MON-FRI", DT(2018_y / 03 / 10, hours{12}, minutes{13}, seconds{45}),
|
||||
{
|
||||
DT(2018_y / 03 / 12, hours{12}),
|
||||
@ -212,4 +221,4 @@ SCENARIO("Examples from README.md")
|
||||
SCENARIO("Unable to calculate time point")
|
||||
{
|
||||
REQUIRE_FALSE(test( "0 0 * 31 FEB *", DT(2021_y / 1 / 1), DT(2022_y / 1 / 1)));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <catch.hpp>
|
||||
#include <libcron/include/libcron/Cron.h>
|
||||
#include <libcron/externals/date/include/date/date.h>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
@ -209,6 +210,7 @@ SCENARIO("Task priority")
|
||||
}
|
||||
AND_WHEN("Waiting based on the time given by the Cron instance")
|
||||
{
|
||||
auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(c.time_until_next());
|
||||
std::this_thread::sleep_for(c.time_until_next());
|
||||
c.tick();
|
||||
|
||||
|
1
test/externals/Catch2
vendored
Submodule
1
test/externals/Catch2
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 5c88067bd339465513af4aec606bd2292f1b594a
|
12851
test/externals/Catch2/single_include/catch.hpp
vendored
12851
test/externals/Catch2/single_include/catch.hpp
vendored
File diff suppressed because it is too large
Load Diff
@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Created by Justin R. Wilson on 2/19/2017.
|
||||
* Copyright 2017 Justin R. Wilson. All rights reserved.
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef TWOBLUECUBES_CATCH_REPORTER_AUTOMAKE_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_REPORTER_AUTOMAKE_HPP_INCLUDED
|
||||
|
||||
// Don't #include any Catch headers here - we can assume they are already
|
||||
// included before this header.
|
||||
// This is not good practice in general but is necessary in this case so this
|
||||
// file can be distributed as a single header that works with the main
|
||||
// Catch single header.
|
||||
|
||||
namespace Catch {
|
||||
|
||||
struct AutomakeReporter : StreamingReporterBase<AutomakeReporter> {
|
||||
AutomakeReporter( ReporterConfig const& _config )
|
||||
: StreamingReporterBase( _config )
|
||||
{}
|
||||
|
||||
~AutomakeReporter() override;
|
||||
|
||||
static std::string getDescription() {
|
||||
return "Reports test results in the format of Automake .trs files";
|
||||
}
|
||||
|
||||
void assertionStarting( AssertionInfo const& ) override {}
|
||||
|
||||
bool assertionEnded( AssertionStats const& /*_assertionStats*/ ) override { return true; }
|
||||
|
||||
void testCaseEnded( TestCaseStats const& _testCaseStats ) override {
|
||||
// Possible values to emit are PASS, XFAIL, SKIP, FAIL, XPASS and ERROR.
|
||||
stream << ":test-result: ";
|
||||
if (_testCaseStats.totals.assertions.allPassed()) {
|
||||
stream << "PASS";
|
||||
} else if (_testCaseStats.totals.assertions.allOk()) {
|
||||
stream << "XFAIL";
|
||||
} else {
|
||||
stream << "FAIL";
|
||||
}
|
||||
stream << ' ' << _testCaseStats.testInfo.name << '\n';
|
||||
StreamingReporterBase::testCaseEnded( _testCaseStats );
|
||||
}
|
||||
|
||||
void skipTest( TestCaseInfo const& testInfo ) override {
|
||||
stream << ":test-result: SKIP " << testInfo.name << '\n';
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#ifdef CATCH_IMPL
|
||||
AutomakeReporter::~AutomakeReporter() {}
|
||||
#endif
|
||||
|
||||
CATCH_REGISTER_REPORTER( "automake", AutomakeReporter)
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_REPORTER_AUTOMAKE_HPP_INCLUDED
|
@ -1,255 +0,0 @@
|
||||
/*
|
||||
* Created by Colton Wolkins on 2015-08-15.
|
||||
* Copyright 2015 Martin Moene. All rights reserved.
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
||||
|
||||
|
||||
// Don't #include any Catch headers here - we can assume they are already
|
||||
// included before this header.
|
||||
// This is not good practice in general but is necessary in this case so this
|
||||
// file can be distributed as a single header that works with the main
|
||||
// Catch single header.
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Catch {
|
||||
|
||||
struct TAPReporter : StreamingReporterBase<TAPReporter> {
|
||||
|
||||
using StreamingReporterBase::StreamingReporterBase;
|
||||
|
||||
~TAPReporter() override;
|
||||
|
||||
static std::string getDescription() {
|
||||
return "Reports test results in TAP format, suitable for test harnesses";
|
||||
}
|
||||
|
||||
ReporterPreferences getPreferences() const override {
|
||||
ReporterPreferences prefs;
|
||||
prefs.shouldRedirectStdOut = false;
|
||||
return prefs;
|
||||
}
|
||||
|
||||
void noMatchingTestCases( std::string const& spec ) override {
|
||||
stream << "# No test cases matched '" << spec << "'" << std::endl;
|
||||
}
|
||||
|
||||
void assertionStarting( AssertionInfo const& ) override {}
|
||||
|
||||
bool assertionEnded( AssertionStats const& _assertionStats ) override {
|
||||
++counter;
|
||||
|
||||
AssertionPrinter printer( stream, _assertionStats, counter );
|
||||
printer.print();
|
||||
stream << " # " << currentTestCaseInfo->name ;
|
||||
|
||||
stream << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void testRunEnded( TestRunStats const& _testRunStats ) override {
|
||||
printTotals( _testRunStats.totals );
|
||||
stream << "\n" << std::endl;
|
||||
StreamingReporterBase::testRunEnded( _testRunStats );
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t counter = 0;
|
||||
class AssertionPrinter {
|
||||
public:
|
||||
AssertionPrinter& operator= ( AssertionPrinter const& ) = delete;
|
||||
AssertionPrinter( AssertionPrinter const& ) = delete;
|
||||
AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, std::size_t _counter )
|
||||
: stream( _stream )
|
||||
, result( _stats.assertionResult )
|
||||
, messages( _stats.infoMessages )
|
||||
, itMessage( _stats.infoMessages.begin() )
|
||||
, printInfoMessages( true )
|
||||
, counter(_counter)
|
||||
{}
|
||||
|
||||
void print() {
|
||||
itMessage = messages.begin();
|
||||
|
||||
switch( result.getResultType() ) {
|
||||
case ResultWas::Ok:
|
||||
printResultType( passedString() );
|
||||
printOriginalExpression();
|
||||
printReconstructedExpression();
|
||||
if ( ! result.hasExpression() )
|
||||
printRemainingMessages( Colour::None );
|
||||
else
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::ExpressionFailed:
|
||||
if (result.isOk()) {
|
||||
printResultType(passedString());
|
||||
} else {
|
||||
printResultType(failedString());
|
||||
}
|
||||
printOriginalExpression();
|
||||
printReconstructedExpression();
|
||||
if (result.isOk()) {
|
||||
printIssue(" # TODO");
|
||||
}
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::ThrewException:
|
||||
printResultType( failedString() );
|
||||
printIssue( "unexpected exception with message:" );
|
||||
printMessage();
|
||||
printExpressionWas();
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::FatalErrorCondition:
|
||||
printResultType( failedString() );
|
||||
printIssue( "fatal error condition with message:" );
|
||||
printMessage();
|
||||
printExpressionWas();
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::DidntThrowException:
|
||||
printResultType( failedString() );
|
||||
printIssue( "expected exception, got none" );
|
||||
printExpressionWas();
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::Info:
|
||||
printResultType( "info" );
|
||||
printMessage();
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::Warning:
|
||||
printResultType( "warning" );
|
||||
printMessage();
|
||||
printRemainingMessages();
|
||||
break;
|
||||
case ResultWas::ExplicitFailure:
|
||||
printResultType( failedString() );
|
||||
printIssue( "explicitly" );
|
||||
printRemainingMessages( Colour::None );
|
||||
break;
|
||||
// These cases are here to prevent compiler warnings
|
||||
case ResultWas::Unknown:
|
||||
case ResultWas::FailureBit:
|
||||
case ResultWas::Exception:
|
||||
printResultType( "** internal error **" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static Colour::Code dimColour() { return Colour::FileName; }
|
||||
|
||||
static const char* failedString() { return "not ok"; }
|
||||
static const char* passedString() { return "ok"; }
|
||||
|
||||
void printSourceInfo() const {
|
||||
Colour colourGuard( dimColour() );
|
||||
stream << result.getSourceInfo() << ":";
|
||||
}
|
||||
|
||||
void printResultType( std::string const& passOrFail ) const {
|
||||
if( !passOrFail.empty() ) {
|
||||
stream << passOrFail << ' ' << counter << " -";
|
||||
}
|
||||
}
|
||||
|
||||
void printIssue( std::string const& issue ) const {
|
||||
stream << " " << issue;
|
||||
}
|
||||
|
||||
void printExpressionWas() {
|
||||
if( result.hasExpression() ) {
|
||||
stream << ";";
|
||||
{
|
||||
Colour colour( dimColour() );
|
||||
stream << " expression was:";
|
||||
}
|
||||
printOriginalExpression();
|
||||
}
|
||||
}
|
||||
|
||||
void printOriginalExpression() const {
|
||||
if( result.hasExpression() ) {
|
||||
stream << " " << result.getExpression();
|
||||
}
|
||||
}
|
||||
|
||||
void printReconstructedExpression() const {
|
||||
if( result.hasExpandedExpression() ) {
|
||||
{
|
||||
Colour colour( dimColour() );
|
||||
stream << " for: ";
|
||||
}
|
||||
std::string expr = result.getExpandedExpression();
|
||||
std::replace( expr.begin(), expr.end(), '\n', ' ');
|
||||
stream << expr;
|
||||
}
|
||||
}
|
||||
|
||||
void printMessage() {
|
||||
if ( itMessage != messages.end() ) {
|
||||
stream << " '" << itMessage->message << "'";
|
||||
++itMessage;
|
||||
}
|
||||
}
|
||||
|
||||
void printRemainingMessages( Colour::Code colour = dimColour() ) {
|
||||
if (itMessage == messages.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// using messages.end() directly (or auto) yields compilation error:
|
||||
std::vector<MessageInfo>::const_iterator itEnd = messages.end();
|
||||
const std::size_t N = static_cast<std::size_t>( std::distance( itMessage, itEnd ) );
|
||||
|
||||
{
|
||||
Colour colourGuard( colour );
|
||||
stream << " with " << pluralise( N, "message" ) << ":";
|
||||
}
|
||||
|
||||
for(; itMessage != itEnd; ) {
|
||||
// If this assertion is a warning ignore any INFO messages
|
||||
if( printInfoMessages || itMessage->type != ResultWas::Info ) {
|
||||
stream << " '" << itMessage->message << "'";
|
||||
if ( ++itMessage != itEnd ) {
|
||||
Colour colourGuard( dimColour() );
|
||||
stream << " and";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream& stream;
|
||||
AssertionResult const& result;
|
||||
std::vector<MessageInfo> messages;
|
||||
std::vector<MessageInfo>::const_iterator itMessage;
|
||||
bool printInfoMessages;
|
||||
std::size_t counter;
|
||||
};
|
||||
|
||||
void printTotals( const Totals& totals ) const {
|
||||
if( totals.testCases.total() == 0 ) {
|
||||
stream << "1..0 # Skipped: No tests ran.";
|
||||
} else {
|
||||
stream << "1.." << counter;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef CATCH_IMPL
|
||||
TAPReporter::~TAPReporter() {}
|
||||
#endif
|
||||
|
||||
CATCH_REGISTER_REPORTER( "tap", TAPReporter )
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
@ -1,220 +0,0 @@
|
||||
/*
|
||||
* Created by Phil Nash on 19th December 2014
|
||||
* Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
||||
|
||||
// Don't #include any Catch headers here - we can assume they are already
|
||||
// included before this header.
|
||||
// This is not good practice in general but is necessary in this case so this
|
||||
// file can be distributed as a single header that works with the main
|
||||
// Catch single header.
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#ifdef __clang__
|
||||
# pragma clang diagnostic push
|
||||
# pragma clang diagnostic ignored "-Wpadded"
|
||||
#endif
|
||||
|
||||
namespace Catch {
|
||||
|
||||
struct TeamCityReporter : StreamingReporterBase<TeamCityReporter> {
|
||||
TeamCityReporter( ReporterConfig const& _config )
|
||||
: StreamingReporterBase( _config )
|
||||
{
|
||||
m_reporterPrefs.shouldRedirectStdOut = true;
|
||||
}
|
||||
|
||||
static std::string escape( std::string const& str ) {
|
||||
std::string escaped = str;
|
||||
replaceInPlace( escaped, "|", "||" );
|
||||
replaceInPlace( escaped, "'", "|'" );
|
||||
replaceInPlace( escaped, "\n", "|n" );
|
||||
replaceInPlace( escaped, "\r", "|r" );
|
||||
replaceInPlace( escaped, "[", "|[" );
|
||||
replaceInPlace( escaped, "]", "|]" );
|
||||
return escaped;
|
||||
}
|
||||
~TeamCityReporter() override;
|
||||
|
||||
static std::string getDescription() {
|
||||
return "Reports test results as TeamCity service messages";
|
||||
}
|
||||
|
||||
void skipTest( TestCaseInfo const& /* testInfo */ ) override {
|
||||
}
|
||||
|
||||
void noMatchingTestCases( std::string const& /* spec */ ) override {}
|
||||
|
||||
void testGroupStarting( GroupInfo const& groupInfo ) override {
|
||||
StreamingReporterBase::testGroupStarting( groupInfo );
|
||||
stream << "##teamcity[testSuiteStarted name='"
|
||||
<< escape( groupInfo.name ) << "']\n";
|
||||
}
|
||||
void testGroupEnded( TestGroupStats const& testGroupStats ) override {
|
||||
StreamingReporterBase::testGroupEnded( testGroupStats );
|
||||
stream << "##teamcity[testSuiteFinished name='"
|
||||
<< escape( testGroupStats.groupInfo.name ) << "']\n";
|
||||
}
|
||||
|
||||
|
||||
void assertionStarting( AssertionInfo const& ) override {}
|
||||
|
||||
bool assertionEnded( AssertionStats const& assertionStats ) override {
|
||||
AssertionResult const& result = assertionStats.assertionResult;
|
||||
if( !result.isOk() ) {
|
||||
|
||||
ReusableStringStream msg;
|
||||
if( !m_headerPrintedForThisSection )
|
||||
printSectionHeader( msg.get() );
|
||||
m_headerPrintedForThisSection = true;
|
||||
|
||||
msg << result.getSourceInfo() << "\n";
|
||||
|
||||
switch( result.getResultType() ) {
|
||||
case ResultWas::ExpressionFailed:
|
||||
msg << "expression failed";
|
||||
break;
|
||||
case ResultWas::ThrewException:
|
||||
msg << "unexpected exception";
|
||||
break;
|
||||
case ResultWas::FatalErrorCondition:
|
||||
msg << "fatal error condition";
|
||||
break;
|
||||
case ResultWas::DidntThrowException:
|
||||
msg << "no exception was thrown where one was expected";
|
||||
break;
|
||||
case ResultWas::ExplicitFailure:
|
||||
msg << "explicit failure";
|
||||
break;
|
||||
|
||||
// We shouldn't get here because of the isOk() test
|
||||
case ResultWas::Ok:
|
||||
case ResultWas::Info:
|
||||
case ResultWas::Warning:
|
||||
throw std::domain_error( "Internal error in TeamCity reporter" );
|
||||
// These cases are here to prevent compiler warnings
|
||||
case ResultWas::Unknown:
|
||||
case ResultWas::FailureBit:
|
||||
case ResultWas::Exception:
|
||||
throw std::domain_error( "Not implemented" );
|
||||
}
|
||||
if( assertionStats.infoMessages.size() == 1 )
|
||||
msg << " with message:";
|
||||
if( assertionStats.infoMessages.size() > 1 )
|
||||
msg << " with messages:";
|
||||
for( auto const& messageInfo : assertionStats.infoMessages )
|
||||
msg << "\n \"" << messageInfo.message << "\"";
|
||||
|
||||
|
||||
if( result.hasExpression() ) {
|
||||
msg <<
|
||||
"\n " << result.getExpressionInMacro() << "\n"
|
||||
"with expansion:\n" <<
|
||||
" " << result.getExpandedExpression() << "\n";
|
||||
}
|
||||
|
||||
if( currentTestCaseInfo->okToFail() ) {
|
||||
msg << "- failure ignore as test marked as 'ok to fail'\n";
|
||||
stream << "##teamcity[testIgnored"
|
||||
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
||||
<< " message='" << escape( msg.str() ) << "'"
|
||||
<< "]\n";
|
||||
}
|
||||
else {
|
||||
stream << "##teamcity[testFailed"
|
||||
<< " name='" << escape( currentTestCaseInfo->name )<< "'"
|
||||
<< " message='" << escape( msg.str() ) << "'"
|
||||
<< "]\n";
|
||||
}
|
||||
}
|
||||
stream.flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
void sectionStarting( SectionInfo const& sectionInfo ) override {
|
||||
m_headerPrintedForThisSection = false;
|
||||
StreamingReporterBase::sectionStarting( sectionInfo );
|
||||
}
|
||||
|
||||
void testCaseStarting( TestCaseInfo const& testInfo ) override {
|
||||
m_testTimer.start();
|
||||
StreamingReporterBase::testCaseStarting( testInfo );
|
||||
stream << "##teamcity[testStarted name='"
|
||||
<< escape( testInfo.name ) << "']\n";
|
||||
stream.flush();
|
||||
}
|
||||
|
||||
void testCaseEnded( TestCaseStats const& testCaseStats ) override {
|
||||
StreamingReporterBase::testCaseEnded( testCaseStats );
|
||||
if( !testCaseStats.stdOut.empty() )
|
||||
stream << "##teamcity[testStdOut name='"
|
||||
<< escape( testCaseStats.testInfo.name )
|
||||
<< "' out='" << escape( testCaseStats.stdOut ) << "']\n";
|
||||
if( !testCaseStats.stdErr.empty() )
|
||||
stream << "##teamcity[testStdErr name='"
|
||||
<< escape( testCaseStats.testInfo.name )
|
||||
<< "' out='" << escape( testCaseStats.stdErr ) << "']\n";
|
||||
stream << "##teamcity[testFinished name='"
|
||||
<< escape( testCaseStats.testInfo.name ) << "' duration='"
|
||||
<< m_testTimer.getElapsedMilliseconds() << "']\n";
|
||||
stream.flush();
|
||||
}
|
||||
|
||||
private:
|
||||
void printSectionHeader( std::ostream& os ) {
|
||||
assert( !m_sectionStack.empty() );
|
||||
|
||||
if( m_sectionStack.size() > 1 ) {
|
||||
os << getLineOfChars<'-'>() << "\n";
|
||||
|
||||
std::vector<SectionInfo>::const_iterator
|
||||
it = m_sectionStack.begin()+1, // Skip first section (test case)
|
||||
itEnd = m_sectionStack.end();
|
||||
for( ; it != itEnd; ++it )
|
||||
printHeaderString( os, it->name );
|
||||
os << getLineOfChars<'-'>() << "\n";
|
||||
}
|
||||
|
||||
SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
|
||||
|
||||
if( !lineInfo.empty() )
|
||||
os << lineInfo << "\n";
|
||||
os << getLineOfChars<'.'>() << "\n\n";
|
||||
}
|
||||
|
||||
// if string has a : in first line will set indent to follow it on
|
||||
// subsequent lines
|
||||
static void printHeaderString( std::ostream& os, std::string const& _string, std::size_t indent = 0 ) {
|
||||
std::size_t i = _string.find( ": " );
|
||||
if( i != std::string::npos )
|
||||
i+=2;
|
||||
else
|
||||
i = 0;
|
||||
os << Column( _string )
|
||||
.indent( indent+i)
|
||||
.initialIndent( indent ) << "\n";
|
||||
}
|
||||
private:
|
||||
bool m_headerPrintedForThisSection = false;
|
||||
Timer m_testTimer;
|
||||
};
|
||||
|
||||
#ifdef CATCH_IMPL
|
||||
TeamCityReporter::~TeamCityReporter() {}
|
||||
#endif
|
||||
|
||||
CATCH_REGISTER_REPORTER( "teamcity", TeamCityReporter )
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#ifdef __clang__
|
||||
# pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
|
Loading…
x
Reference in New Issue
Block a user