- Adding default pretty-printer for Task

- Adding equivalency check for Task
- Adding test to ensure serialization / deserialization is reproducible.
This commit is contained in:
Ian Roddis
2021-08-11 10:16:38 -03:00
parent a152588368
commit 4d519cc596
4 changed files with 32 additions and 4 deletions

View File

@@ -26,4 +26,7 @@ namespace daggy {
std::string taskToJSON(const Task &task);
std::string tasksToJSON(const std::vector<Task> &tasks);
// default serialization
std::ostream &operator<<(std::ostream &os, const Task &task);
}

View File

@@ -12,5 +12,13 @@ namespace daggy {
uint32_t maxRetries;
uint32_t retryIntervalSeconds; // Time to wait between retries
std::unordered_set<std::string> children;
bool operator==(const Task &other) const {
return (name == other.name)
and (maxRetries == other.maxRetries)
and (retryIntervalSeconds == other.retryIntervalSeconds)
and (command == other.command)
and (children == other.children);
}
};
}

View File

@@ -111,10 +111,10 @@ namespace daggy {
// Create the tasks
auto &taskNames = childrenMap[name];
for (size_t tid = 0; tid < commands.size(); ++tid) {
std::string taskName = name + "_" + std::to_string(tid);
std::string taskName = (commands.size() == 1 ? name : name + "_" + std::to_string(tid));
taskNames.push_back(taskName);
tasks.emplace_back(Task{
.name = name + "_" + std::to_string(tid),
.name = taskName,
.command = commands[tid],
.maxRetries = maxRetries,
.retryIntervalSeconds = retryIntervalSeconds,
@@ -195,4 +195,9 @@ namespace daggy {
return ss.str();
}
std::ostream &operator<<(std::ostream &os, const Task &task) {
os << taskToJSON(task);
return os;
}
}

View File

@@ -55,8 +55,20 @@ TEST_CASE("Task Serialization", "[serialize_tasks]") {
SECTION("Build with no expansion") {
std::string testTasks = R"([{"name": "A", "command": ["/bin/echo", "A"], "children": ["C"]}, {"name": "B", "command": ["/bin/echo", "B"], "children": ["C"]},{"name": "C", "command": ["/bin/echo", "C"]}])";
auto tasks = daggy::tasksFromJSON(testTasks);
std::unordered_map<std::string, size_t> taskMap;
for (size_t i = 0; i < tasks.size(); ++i) {
taskMap[tasks[i].name] = i;
}
auto genJSON = daggy::tasksToJSON(tasks);
std::cout << genJSON << std::endl;
REQUIRE_NOTHROW(daggy::tasksFromJSON(genJSON));
auto regenTasks = daggy::tasksFromJSON(genJSON);
REQUIRE(regenTasks.size() == tasks.size());
for (const auto &task : regenTasks) {
const auto &other = tasks[taskMap[task.name]];
REQUIRE(task == other);
}
}
}