Commit dd136a20 authored by Noah Orensa's avatar Noah Orensa
Browse files

implement a method to set global dependencies of all tests under some module

parent b820208c
......@@ -16,6 +16,11 @@ using Status = dtest::Test::Status;
////
#define setDependencies(module, dependencies) static auto __dtest_concat(__dependency_injector__uid_, __COUNTER__) = \
dtest::Test::DependencyInjector(module, dependencies)
////
#include <dtest_core/unit_test.h>
#ifdef DTEST_DISABLE_ALL
......
......@@ -41,6 +41,7 @@ protected:
std::string _name;
std::unordered_set<std::string> _dependencies;
std::unordered_set<std::string> _remainingDependencies;
bool _enabled = true;
bool _success = false;
......@@ -78,21 +79,23 @@ private:
public:
inline Test(
const std::string &module,
const std::string &name
): _module("root"),
): _module(module),
_name(name)
{
__tests[_module].push_back(this);
auto it = __globalDependencies.find(module);
if (it != __globalDependencies.end()) {
_dependencies = it->second;
}
}
inline Test(
const std::string &module,
const std::string &name
): _module(module),
_name(name)
{
__tests[_module].push_back(this);
}
): Test("root", name)
{ }
virtual Test * copy() const = 0;
......@@ -137,6 +140,8 @@ private:
static std::unordered_map<std::string, std::list<Test *>> __tests;
static std::unordered_map<std::string, std::unordered_set<std::string>> __globalDependencies;
static bool _logStatsToStderr;
static bool _isDriver;
......@@ -145,6 +150,20 @@ private:
public:
struct DependencyInjector {
DependencyInjector(
const std::string &module,
const std::initializer_list<std::string> &dependencies
) {
Test::setGlobalModuleDependencies(module, dependencies);
}
};
static void setGlobalModuleDependencies(
const std::string &module,
const std::initializer_list<std::string> &dependencies
);
static inline const std::string & statusString(const Status &status) {
return __statusString[(uint32_t) status];
}
......
#pragma once
#include <string>
#include <vector>
#include <sstream>
std::string formatDuration(double nanos);
std::string formatDurationJSON(double nanos);
......@@ -12,4 +12,27 @@ std::string indent(const std::string &str, int spaces);
std::string jsonify(const std::string &str);
std::string jsonify(size_t count, char const * const *str, int indent = 0);
std::string jsonify(const std::vector<std::string> &str, int indent = 0);
template <typename Container>
std::string jsonify(const Container &str, int indent = 0) {
std::stringstream s;
std::string in = "";
in.resize(indent, ' ');
size_t count = str.size();
s << '[';
if (count > 0) s << '\n' << in;
else s << ' ';
auto it = str.begin();
for (size_t i = 0; it != str.end(); ++it, ++i) {
s << " \"" << *it;
if (i < count - 1) s << "\",\n" << in;
else s << "\"\n" << in;
}
s << ']';
return s.str();
}
......@@ -67,6 +67,8 @@ static std::string __statusStringPastTense[] = {
std::unordered_map<std::string, std::list<Test *>> Test::__tests;
std::unordered_map<std::string, std::unordered_set<std::string>> Test::__globalDependencies;
bool Test::_logStatsToStderr = false;
bool Test::_isDriver = false;
......@@ -90,6 +92,21 @@ std::string Test::_errorReport() {
return s.str();
}
void Test::setGlobalModuleDependencies(
const std::string &module,
const std::initializer_list<std::string> &dependencies
) {
__globalDependencies[module] = dependencies;
for (auto t : __tests[module]) {
for (const auto &dep : dependencies) {
if (t->_dependencies.count(dep) == 0) {
t->_dependencies.insert(dep);
}
}
}
}
bool Test::runAll(
const std::vector<std::pair<std::string, std::string>> &config,
std::ostream &out
......@@ -116,6 +133,7 @@ bool Test::runAll(
ready.push_back(tt);
}
else {
tt->_remainingDependencies = t->_dependencies;
for (const auto &dep: t->_dependencies) {
blocked[dep].push_back(tt);
}
......@@ -197,8 +215,11 @@ bool Test::runAll(
}
out << "\n {";
out << "\n \"i\": " << runCount << ",";
out << "\n \"name\": \"" << testname << "\",";
if (! test->_dependencies.empty()) {
out << "\n \"dependencies\": " << jsonify(test->_dependencies, 6) << ",";
}
out << "\n \"i\": " << runCount << ",";
out << "\n \"success\": " << test->_success << ",";
out << "\n \"status\": \"" << statusString(test->_status) << "\",";
out << "\n \"report\": {\n" << indent(test->_detailedReport, 8);
......@@ -231,9 +252,9 @@ bool Test::runAll(
auto insertPos = ready.begin();
for (auto tt : it->second) {
// and remove that module from their set of dependencies
tt->_dependencies.erase(test->_module);
tt->_remainingDependencies.erase(test->_module);
// if no more dependencies are needed, then push to ready queue
if (tt->_dependencies.empty()) ready.insert(insertPos, tt);
if (tt->_remainingDependencies.empty()) ready.insert(insertPos, tt);
}
}
}
......
#include <dtest_core/util.h>
#include <sstream>
std::string formatDuration(double nanos) {
std::stringstream s;
......@@ -105,24 +104,3 @@ std::string jsonify(size_t count, char const * const *str, int indent) {
return s.str();
}
std::string jsonify(const std::vector<std::string> &str, int indent) {
std::stringstream s;
std::string in = "";
in.resize(indent, ' ');
size_t count = str.size();
s << '[';
if (count > 0) s << '\n' << in;
else s << ' ';
for (size_t i = 0; i < count; ++i) {
s << " \"" << str[i];
if (i < count - 1) s << "\",\n" << in;
else s << "\"\n" << in;
}
s << ']';
return s.str();
}
#include <dtest.h>
setDependencies("distributed-unit-test", { "unit-test" });
dunit("distributed-unit-test", "pass")
.dependsOn("unit-test")
.driver([] {
assert(true)
})
......@@ -10,7 +11,6 @@ dunit("distributed-unit-test", "pass")
});
dunit("distributed-unit-test", "fail")
.dependsOn("unit-test")
.expect(Status::FAIL)
.driver([] {
assert(false)
......@@ -20,7 +20,6 @@ dunit("distributed-unit-test", "fail")
});
dunit("distributed-unit-test", "driver-fail")
.dependsOn("unit-test")
.expect(Status::FAIL)
.driver([] {
assert(false)
......@@ -30,7 +29,6 @@ dunit("distributed-unit-test", "driver-fail")
});
dunit("distributed-unit-test", "worker-fail")
.dependsOn("unit-test")
.expect(Status::FAIL)
.driver([] {
assert(true)
......@@ -40,7 +38,6 @@ dunit("distributed-unit-test", "worker-fail")
});
dunit("distributed-unit-test", "mem-leak")
.dependsOn("unit-test")
.expect(Status::PASS_WITH_MEMORY_LEAK)
.driver([] {
#pragma GCC diagnostic push
......@@ -58,7 +55,6 @@ dunit("distributed-unit-test", "mem-leak")
});
dunit("distributed-unit-test", "driver-mem-leak")
.dependsOn("unit-test")
.expect(Status::PASS_WITH_MEMORY_LEAK)
.driver([] {
#pragma GCC diagnostic push
......@@ -72,7 +68,6 @@ dunit("distributed-unit-test", "driver-mem-leak")
});
dunit("distributed-unit-test", "worker-mem-leak")
.dependsOn("unit-test")
.expect(Status::PASS_WITH_MEMORY_LEAK)
.driver([] {
assert(true);
......@@ -86,7 +81,6 @@ dunit("distributed-unit-test", "worker-mem-leak")
});
dunit("distributed-unit-test", "wait-notify")
.dependsOn("unit-test")
.workers(4)
.driver([] {
dtest_notify();
......@@ -98,7 +92,6 @@ dunit("distributed-unit-test", "wait-notify")
});
dunit("distributed-unit-test", "user-message")
.dependsOn("unit-test")
.workers(4)
.driver([] {
int x;
......@@ -165,7 +158,6 @@ static sockaddr get_sock_addr(int fd) {
}
dunit("distributed-unit-test", "tcp")
.dependsOn("unit-test")
.workers(1)
.driver([] {
auto fd = tcp_server_sock();
......@@ -191,7 +183,6 @@ dunit("distributed-unit-test", "tcp")
});
dunit("distributed-unit-test", "tcp-faulty")
.dependsOn("unit-test")
.workers(1)
.faultyNetwork(0)
.driver([] {
......@@ -220,7 +211,6 @@ dunit("distributed-unit-test", "tcp-faulty")
});
dunit("distributed-unit-test", "udp")
.dependsOn("unit-test")
.workers(1)
.driver([] {
auto fd = udp_server_sock();
......@@ -244,7 +234,6 @@ dunit("distributed-unit-test", "udp")
});
dunit("distributed-unit-test", "udp-faulty")
.dependsOn("unit-test")
.workers(1)
.faultyNetwork(0.3, 0)
.driver([] {
......
#include <dtest.h>
setDependencies("performance-test", { "unit-test" });
perf("performance-test", "pass")
.dependsOn("unit-test")
.body([] {
for (int i = 0; i < 1000000; ++i);
})
......@@ -10,7 +11,6 @@ perf("performance-test", "pass")
});
perf("performance-test", "too-slow")
.dependsOn("unit-test")
.expect(Status::TOO_SLOW)
.body([] {
for (int i = 0; i < 8000000; ++i);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment