Browse Source

Tests: PityUnit - Fixes and renames

master
heck 4 years ago
parent
commit
e4f0efe82d
  1. 46
      src/PityUnit.hh
  2. 229
      src/PityUnit.hxx

46
src/PityUnit.hh

@ -16,8 +16,6 @@ namespace pEp {
template<class T = void> template<class T = void>
class PityUnit { class PityUnit {
public: public:
using NodeFunc = std::function<void(const PityUnit&)>;
enum class ExecutionMode enum class ExecutionMode
{ {
FUNCTION, FUNCTION,
@ -30,19 +28,18 @@ namespace pEp {
// Constructors are private // Constructors are private
PityUnit() = delete; PityUnit() = delete;
explicit PityUnit( explicit PityUnit<T>(
PityUnit* parent, PityUnit<T>* parent,
const std::string& name, const std::string& name,
const NodeFunc test_func = nullptr, const std::function<void(const PityUnit&)> test_func = nullptr,
T* model = nullptr, T* model = nullptr,
ExecutionMode exec_mode = ExecutionMode::FUNCTION); ExecutionMode exec_mode = ExecutionMode::FUNCTION);
// Read-Only // Read-Only
std::string getNodeName() const; std::string getName() const;
std::string getNodePath() const; std::string getPath() const;
std::string getNodePathShort() const; std::string getPathShort() const;
T* getModel() const; T* getModel() const;
std::string rootNodeDir() const;
std::string processDir() const; // own process dir std::string processDir() const; // own process dir
// Read-Write // Read-Write
@ -52,14 +49,15 @@ namespace pEp {
// Main funcs // Main funcs
void _init() const;
void run() const; void run() const;
std::string to_string(bool recursive = true, int indent = 0) const; std::string to_string(bool recursive = true, int indent = 0) const;
static std::string to_string(const ExecutionMode& emode); static std::string to_string(const ExecutionMode& emode);
// Util
void recreateDirsRecursively() const;
// logging service // logging service
void log(const std::string& msg) const; void log(const std::string& msg) const;
void logRaw(const std::string& msg) const;
void logH1(const std::string& msg) const; void logH1(const std::string& msg) const;
void logH2(const std::string& msg) const; void logH2(const std::string& msg) const;
void logH3(const std::string& msg) const; void logH3(const std::string& msg) const;
@ -73,7 +71,7 @@ namespace pEp {
// METHODS // METHODS
// Execution // Execution
void _runRootNode() const; void _init() const;
void _run() const; void _run() const;
void _runSelf() const; void _runSelf() const;
void _runChildren() const; void _runChildren() const;
@ -81,35 +79,37 @@ namespace pEp {
void _waitChildProcesses() const; void _waitChildProcesses() const;
// Modify // Modify
void _addChildNode(PityUnit& node); void _addChildUnit(PityUnit& unit);
// Query // Query
bool _isProcessNode() const; bool _isProcessUnit() const;
bool _isRootNode() const; bool _isRootUnit() const;
const PityUnit& _rootNode() const; const PityUnit& _rootUnit() const;
const PityUnit& _parentingProcessNode() const; std::string _rootUnitDir() const;
const PityUnit& _parentingProcessUnit() const;
// Util // Util
std::string _normalizeName(std::string name) const; std::string _normalizeName(std::string name) const;
std::string _status_string(const std::string& msg) const; std::string _status_string(const std::string& msg) const;
Utils::Color colForProcNodeNr(int procNodeNr) const; Utils::Color _colForProcUnitNr(int procUnitNr) const;
Utils::Color termColor() const; Utils::Color _termColor() const;
void logRaw(const std::string& msg) const;
// Dirs // Dirs
void _ensureDir(const std::string& path) const; void _ensureDir(const std::string& path) const;
void _recreateDir(const std::string& path) const; void _recreateDir(const std::string& path) const;
// Fields // Fields
const std::string _name; const std::string _name;
const PityUnit* _parent; //nullptr if RootUnit const PityUnit* _parent; //nullptr if RootUnit
T* _model; //nullptr if inherited T* _model; //nullptr if inherited
const NodeFunc _test_func; const std::function<void(const PityUnit&)> _test_func;
ExecutionMode _exec_mode; ExecutionMode _exec_mode;
static std::string _global_root_dir; static std::string _global_root_dir;
std::map<const std::string, PityUnit&> _children; // map to guarantee uniqueness of sibling-names std::map<const std::string, PityUnit&> _children; // map to guarantee uniqueness of sibling-names
int procNodeNr; int procUnitNr;
static int procNodesCount; // will be increased in everuy constructor static int procUnitsCount; // will be increased in everuy constructor
// internal logging // internal logging
Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug;

229
src/PityUnit.hxx

@ -19,77 +19,82 @@
namespace pEp { namespace pEp {
namespace PityTest11 { namespace PityTest11 {
// static
template<class T> template<class T>
std::string PityUnit<T>::_global_root_dir = "./pitytest_data/"; std::string PityUnit<T>::_global_root_dir = "./pitytest_data/";
// static
template<class T> template<class T>
bool PityUnit<T>::debug_log_enabled = false; bool PityUnit<T>::debug_log_enabled = false;
// static
template<class T> template<class T>
int PityUnit<T>::procNodesCount = 0; int PityUnit<T>::procUnitsCount = 0;
// CONCSTRUCTORS // CONSTRUCTOR
template<class T> template<class T>
PityUnit<T>::PityUnit( PityUnit<T>::PityUnit(
PityUnit* const parent, PityUnit<T>* const parent,
const std::string& name, const std::string& name,
const NodeFunc test_func, const std::function<void(const PityUnit&)> test_func,
T* model, T* model,
ExecutionMode exec_mode) : ExecutionMode exec_mode) :
_parent(parent), _parent{ parent },
_model(model), _name(_normalizeName(name)), _test_func(test_func), _exec_mode(exec_mode) _model{ model }, _name{ _normalizeName(name) }, _test_func{ test_func }, _exec_mode{
exec_mode
}
{ {
logger_debug.set_instancename(getNodePath()); logger_debug.set_instancename(getPath());
if (!_isRootNode()) { if (!_isRootUnit()) {
parent->_addChildNode(*this); parent->_addChildUnit(*this);
// Inherit // Inherit
procNodeNr = _parent->procNodeNr; procUnitNr = _parent->procUnitNr;
//Or update if procNode //Or update if procUnit
if (_isProcessNode()) { if (_isProcessUnit()) {
procNodesCount++; procUnitsCount++;
procNodeNr = procNodesCount; procUnitNr = procUnitsCount;
} }
} else { } else {
procNodeNr = procNodesCount; procUnitNr = procUnitsCount;
} }
} }
template<class T> template<class T>
std::string PityUnit<T>::getNodeName() const std::string PityUnit<T>::getName() const
{ {
return _name; return _name;
} }
template<class T> template<class T>
std::string PityUnit<T>::getNodePath() const std::string PityUnit<T>::getPath() const
{ {
std::string ret; std::string ret;
if (!_isRootNode()) { if (!_isRootUnit()) {
ret = _parent->getNodePath() + "/" + getNodeName(); ret = _parent->getPath() + "/" + getName();
} else { } else {
ret = getNodeName(); ret = getName();
} }
return ret; return ret;
} }
// For: // For:
// RootNode - "<name>" // RootUnit - "<name>"
// ProcessNode - ".../<proc>" // ProcessUnit - ".../<proc>"
// When Process as dir. parent - ".../<proc>/name" // When Process as dir. parent - ".../<proc>/name"
// When no process as dir. parent - ".../<proc>/.../name" // When no process as dir. parent - ".../<proc>/.../name"
template<class T> template<class T>
std::string PityUnit<T>::getNodePathShort() const std::string PityUnit<T>::getPathShort() const
{ {
std::string ret; std::string ret;
if (_isRootNode()) { if (_isRootUnit()) {
ret = getNodeName(); ret = getName();
} else { } else {
if (_isProcessNode()) { if (_isProcessUnit()) {
ret += ".../" + getNodeName(); ret += ".../" + getName();
} else { } else {
if (&(_parentingProcessNode()) == (_parent)) { if (&(_parentingProcessUnit()) == (_parent)) {
ret = _parentingProcessNode().getNodePathShort() + "/" + getNodeName(); ret = _parentingProcessUnit().getPathShort() + "/" + getName();
} else { } else {
ret = _parentingProcessNode().getNodePathShort() + "/.../" + getNodeName(); ret = _parentingProcessUnit().getPathShort() + "/.../" + getName();
} }
} }
} }
@ -106,30 +111,30 @@ namespace pEp {
if (_model != nullptr) { if (_model != nullptr) {
ret = _model; ret = _model;
} else { } else {
if (!_isRootNode()) { if (!_isRootUnit()) {
ret = _parent->getModel(); ret = _parent->getModel();
} }
} }
return ret; return ret;
} }
// Every RootNode has its own dir // Every RootUnit has its own dir
template<class T> template<class T>
std::string PityUnit<T>::rootNodeDir() const std::string PityUnit<T>::_rootUnitDir() const
{ {
return getGlobalRootDir() + _rootNode().getNodeName() + "/"; return getGlobalRootDir() + _rootUnit().getName() + "/";
} }
// Every process has its own dir inside its rootNodeDir // Every process has its own dir inside its rootUnitDir
// All other nodes inherit processDir from their Root/ProcessNode // All other units inherit processDir from their Root/ProcessUnit
template<class T> template<class T>
std::string PityUnit<T>::processDir() const std::string PityUnit<T>::processDir() const
{ {
if (_isRootNode()) { if (_isRootUnit()) {
return rootNodeDir(); return _rootUnitDir();
} else { } else {
if (_isProcessNode()) { if (_isProcessUnit()) {
return rootNodeDir() + getNodeName() + "/"; return _rootUnitDir() + getName() + "/";
} else { } else {
return _parent->processDir(); return _parent->processDir();
} }
@ -143,10 +148,10 @@ namespace pEp {
// } // }
// static // static
template<> template<class T>
void PityUnit<void>::setGlobalRootDir(const std::string& dir) void PityUnit<T>::setGlobalRootDir(const std::string& dir)
{ {
PityUnit::_global_root_dir = dir; PityUnit<T>::_global_root_dir = dir;
} }
// static // static
@ -156,47 +161,23 @@ namespace pEp {
return PityUnit::_global_root_dir; return PityUnit::_global_root_dir;
} }
template<class T>
void PityUnit<T>::_init() const
{
logH1("PityTest Starting...");
logRaw("RootNode: " + getNodePathShort());
logRaw("GlobalRootDir: " + getGlobalRootDir());
logRaw("\nTestTree");
logRaw("--------");
logRaw(to_string());
logH3("INIT");
_ensureDir(getGlobalRootDir());
_recreateDir(processDir());
if (!_children.empty()) {
for (const std::pair<std::string, PityUnit<T>&> child : _children) {
_recreateDir(child.second.processDir());
}
}
logH3("INIT DONE");
}
template<class T> template<class T>
void PityUnit<T>::run() const void PityUnit<T>::run() const
{ {
pEpLogClass("called"); pEpLogClass("called");
// caller is never nullptr if called by another PityUnit // caller is never nullptr if called by another PityUnit
if (_isRootNode()) { if (_isRootUnit()) {
_init(); _init();
} }
// Execute in fork and wait here until process ends // Execute in fork and wait here until process ends
if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL) { // fork if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL) { // fork
logH2(_status_string("RUNNING")); _executeInFork(std::bind(&PityUnit<T>::_run, this), true);
_executeInFork(std::bind(&PityUnit::_run, this), true);
// Execute in fork and go on, wait for process execution in the end // Execute in fork and go on, wait for process execution in the end
} else if (_exec_mode == ExecutionMode::PROCESS_PARALLEL) { } else if (_exec_mode == ExecutionMode::PROCESS_PARALLEL) {
logH2(_status_string("RUNNING")); _executeInFork(std::bind(&PityUnit<T>::_run, this), false);
_executeInFork(std::bind(&PityUnit::_run, this), false);
// Execute as normal funciton // Execute as normal funciton
} else if (_exec_mode == ExecutionMode::FUNCTION) { } else if (_exec_mode == ExecutionMode::FUNCTION) {
logH3(_status_string("RUNNING"));
_run(); _run();
} else if (_exec_mode == ExecutionMode::THREAD_PARALLEL) { } else if (_exec_mode == ExecutionMode::THREAD_PARALLEL) {
throw std::invalid_argument(to_string(_exec_mode) + " - not implemented"); throw std::invalid_argument(to_string(_exec_mode) + " - not implemented");
@ -204,7 +185,7 @@ namespace pEp {
throw std::invalid_argument(to_string(_exec_mode) + " - not implemented"); throw std::invalid_argument(to_string(_exec_mode) + " - not implemented");
} }
if (_isRootNode()) { if (_isRootUnit()) {
_waitChildProcesses(); _waitChildProcesses();
} }
} }
@ -216,7 +197,7 @@ namespace pEp {
std::stringstream builder; std::stringstream builder;
builder << std::string(indent * 4, ' '); builder << std::string(indent * 4, ' ');
builder << getNodeName(); builder << getName();
builder << " [ "; builder << " [ ";
builder << to_string(_exec_mode) << " - "; builder << to_string(_exec_mode) << " - ";
builder << "\"" << processDir() << "\""; builder << "\"" << processDir() << "\"";
@ -225,11 +206,13 @@ namespace pEp {
ret = builder.str(); ret = builder.str();
if (recursive) { if (recursive) {
indent++; if (!_children.empty()) {
for (const std::pair<std::string, const PityUnit&> child : _children) { indent++;
ret += child.second.to_string(true, indent); for (const std::pair<std::string, const PityUnit<T>&> child : _children) {
ret += child.second.to_string(true, indent);
}
indent--;
} }
indent--;
} }
return ret; return ret;
} }
@ -262,43 +245,60 @@ namespace pEp {
builder << "["; builder << "[";
builder << std::to_string(getpid()); builder << std::to_string(getpid());
builder << " - "; builder << " - ";
builder << getNodePathShort(); builder << getPathShort();
builder << "] - "; builder << "] - ";
builder << msg; builder << msg;
logRaw(builder.str()); logRaw(builder.str());
} }
template<class T>
void PityUnit<T>::logRaw(const std::string& msg) const
{
Adapter::pEpLog::log(msg, termColor());
}
template<class T> template<class T>
void PityUnit<T>::logH1(const std::string& msg) const void PityUnit<T>::logH1(const std::string& msg) const
{ {
Adapter::pEpLog::logH1(msg, termColor()); Adapter::pEpLog::logH1(msg, _termColor());
} }
template<class T> template<class T>
void PityUnit<T>::logH2(const std::string& msg) const void PityUnit<T>::logH2(const std::string& msg) const
{ {
Adapter::pEpLog::logH2(msg, termColor()); Adapter::pEpLog::logH2(msg, _termColor());
} }
template<class T> template<class T>
void PityUnit<T>::logH3(const std::string& msg) const void PityUnit<T>::logH3(const std::string& msg) const
{ {
Adapter::pEpLog::logH3(msg, termColor()); Adapter::pEpLog::logH3(msg, _termColor());
} }
// PRIVATE --------------------------------------------------------------------------------- // PRIVATE ---------------------------------------------------------------------------------
template<class T>
void PityUnit<T>::_init() const
{
logH1("PityTest Starting...");
logRaw("RootUnit: " + getPathShort());
logRaw("GlobalRootDir: " + getGlobalRootDir());
logRaw("\nTestTree");
logRaw("--------");
logRaw(to_string());
logH3("INIT");
_ensureDir(getGlobalRootDir());
recreateDirsRecursively();
// if (!_children.empty()) {
// for (const std::pair<std::string, PityUnit<T>&> child : _children) {
// _recreateDir(child.second.processDir());
// }
// }
logH3("INIT DONE");
}
template<class T> template<class T>
void PityUnit<T>::_run() const void PityUnit<T>::_run() const
{ {
logH2(_status_string("STARTING"));
_runSelf(); _runSelf();
_runChildren(); _runChildren();
} }
@ -309,10 +309,11 @@ namespace pEp {
if (_test_func != nullptr) { if (_test_func != nullptr) {
try { try {
_test_func(*this); _test_func(*this);
logH3(_status_string("SUCCESS")); logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_termColor())));
} catch (const std::exception& e) { } catch (const std::exception& e) {
logRaw("reason: " + std::string(e.what())); logRaw("reason: " + std::string(e.what()));
logH3(_status_string("FAILED")); logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_termColor())
));
} }
} else { } else {
logRaw("No function to execute"); logRaw("No function to execute");
@ -323,7 +324,7 @@ namespace pEp {
void PityUnit<T>::_runChildren() const void PityUnit<T>::_runChildren() const
{ {
if (!_children.empty()) { if (!_children.empty()) {
for (const std::pair<std::string, PityUnit&> child : _children) { for (const std::pair<std::string, PityUnit<T>&> child : _children) {
child.second.run(); child.second.run();
} }
} }
@ -358,13 +359,13 @@ namespace pEp {
} }
template<class T> template<class T>
void PityUnit<T>::_addChildNode(PityUnit& node) void PityUnit<T>::_addChildUnit(PityUnit<T>& unit)
{ {
_children.insert(std::pair<std::string, PityUnit&>(node.getNodeName(), node)); _children.insert(std::pair<std::string, PityUnit<T>&>(unit.getName(), unit));
} }
template<class T> template<class T>
bool PityUnit<T>::_isProcessNode() const bool PityUnit<T>::_isProcessUnit() const
{ {
bool ret = false; bool ret = false;
if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL || if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL ||
@ -375,7 +376,7 @@ namespace pEp {
} }
template<class T> template<class T>
bool PityUnit<T>::_isRootNode() const bool PityUnit<T>::_isRootUnit() const
{ {
if (_parent == nullptr) { if (_parent == nullptr) {
return true; return true;
@ -385,27 +386,27 @@ namespace pEp {
} }
template<class T> template<class T>
const PityUnit<T>& PityUnit<T>::_rootNode() const const PityUnit<T>& PityUnit<T>::_rootUnit() const
{ {
const PityUnit* ret = nullptr; const PityUnit<T>* ret = nullptr;
if (!_isRootNode()) { if (!_isRootUnit()) {
ret = &(_parent->_rootNode()); ret = &(_parent->_rootUnit());
} else { } else {
ret = this; ret = this;
} }
assert(ret != nullptr); assert(ret != nullptr);
// cant be null because for createChildNode() you need to provide a TestNode& and // cant be null because for createChildUnit() you need to provide a TestUnit& and
// the only other way is using createRootNode() which has parent == nullptr // the only other way is using createRootUnit() which has parent == nullptr
return *ret; return *ret;
} }
template<class T> template<class T>
const PityUnit<T>& PityUnit<T>::_parentingProcessNode() const const PityUnit<T>& PityUnit<T>::_parentingProcessUnit() const
{ {
if (_isRootNode() || _isProcessNode()) { if (_isRootUnit() || _isProcessUnit()) {
return *this; return *this;
} else { } else {
return _parent->_parentingProcessNode(); return _parent->_parentingProcessUnit();
} }
} }
@ -427,15 +428,15 @@ namespace pEp {
{ {
std::string ret; std::string ret;
ret = "[ " + to_string(_exec_mode) + ":" + std::to_string(getpid()) + " ] [ " + ret = "[ " + to_string(_exec_mode) + ":" + std::to_string(getpid()) + " ] [ " +
getNodePathShort() + " ] [ " + msg + " ]"; getPathShort() + " ] [ " + msg + " ]";
return ret; return ret;
} }
template<class T> template<class T>
Utils::Color PityUnit<T>::colForProcNodeNr(int procNodeNr) const Utils::Color PityUnit<T>::_colForProcUnitNr(int procUnitNr) const
{ {
switch (procNodeNr) { switch (procUnitNr) {
case 0: case 0:
return Utils::Color::WHITE; return Utils::Color::WHITE;
case 1: case 1:
@ -456,9 +457,15 @@ namespace pEp {
} }
template<class T> template<class T>
Utils::Color PityUnit<T>::termColor() const Utils::Color PityUnit<T>::_termColor() const
{ {
return colForProcNodeNr(procNodeNr); return _colForProcUnitNr(procUnitNr);
}
template<class T>
void PityUnit<T>::logRaw(const std::string& msg) const
{
Adapter::pEpLog::log(msg, _termColor());
} }
template<class T> template<class T>
@ -485,6 +492,16 @@ namespace pEp {
Utils::dir_create(path); Utils::dir_create(path);
} }
template<class T>
void PityUnit<T>::recreateDirsRecursively() const
{
_recreateDir(processDir());
if (!_children.empty()) {
for (const std::pair<std::string, PityUnit<T>&> child : _children) {
child.second.recreateDirsRecursively();
}
}
}
} // namespace PityTest11 } // namespace PityTest11
} // namespace pEp } // namespace pEp

Loading…
Cancel
Save