From e4f0efe82d62857d102a097b09461cad2f6e456f Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 7 Jun 2021 23:50:40 +0200 Subject: [PATCH] Tests: PityUnit - Fixes and renames --- src/PityUnit.hh | 46 +++++----- src/PityUnit.hxx | 229 +++++++++++++++++++++++++---------------------- 2 files changed, 146 insertions(+), 129 deletions(-) diff --git a/src/PityUnit.hh b/src/PityUnit.hh index 5e7bd33..ab60365 100644 --- a/src/PityUnit.hh +++ b/src/PityUnit.hh @@ -16,8 +16,6 @@ namespace pEp { template class PityUnit { public: - using NodeFunc = std::function; - enum class ExecutionMode { FUNCTION, @@ -30,19 +28,18 @@ namespace pEp { // Constructors are private PityUnit() = delete; - explicit PityUnit( - PityUnit* parent, + explicit PityUnit( + PityUnit* parent, const std::string& name, - const NodeFunc test_func = nullptr, + const std::function test_func = nullptr, T* model = nullptr, ExecutionMode exec_mode = ExecutionMode::FUNCTION); // Read-Only - std::string getNodeName() const; - std::string getNodePath() const; - std::string getNodePathShort() const; + std::string getName() const; + std::string getPath() const; + std::string getPathShort() const; T* getModel() const; - std::string rootNodeDir() const; std::string processDir() const; // own process dir // Read-Write @@ -52,14 +49,15 @@ namespace pEp { // Main funcs - void _init() const; void run() const; std::string to_string(bool recursive = true, int indent = 0) const; static std::string to_string(const ExecutionMode& emode); + // Util + void recreateDirsRecursively() const; + // logging service void log(const std::string& msg) const; - void logRaw(const std::string& msg) const; void logH1(const std::string& msg) const; void logH2(const std::string& msg) const; void logH3(const std::string& msg) const; @@ -73,7 +71,7 @@ namespace pEp { // METHODS // Execution - void _runRootNode() const; + void _init() const; void _run() const; void _runSelf() const; void _runChildren() const; @@ -81,35 +79,37 @@ namespace pEp { void _waitChildProcesses() const; // Modify - void _addChildNode(PityUnit& node); + void _addChildUnit(PityUnit& unit); // Query - bool _isProcessNode() const; - bool _isRootNode() const; - const PityUnit& _rootNode() const; - const PityUnit& _parentingProcessNode() const; + bool _isProcessUnit() const; + bool _isRootUnit() const; + const PityUnit& _rootUnit() const; + std::string _rootUnitDir() const; + const PityUnit& _parentingProcessUnit() const; // Util std::string _normalizeName(std::string name) const; std::string _status_string(const std::string& msg) const; - Utils::Color colForProcNodeNr(int procNodeNr) const; - Utils::Color termColor() const; + Utils::Color _colForProcUnitNr(int procUnitNr) const; + Utils::Color _termColor() const; + void logRaw(const std::string& msg) const; // Dirs void _ensureDir(const std::string& path) const; void _recreateDir(const std::string& path) const; + // Fields const std::string _name; const PityUnit* _parent; //nullptr if RootUnit T* _model; //nullptr if inherited - const NodeFunc _test_func; + const std::function _test_func; ExecutionMode _exec_mode; static std::string _global_root_dir; std::map _children; // map to guarantee uniqueness of sibling-names - int procNodeNr; - static int procNodesCount; // will be increased in everuy constructor - + int procUnitNr; + static int procUnitsCount; // will be increased in everuy constructor // internal logging Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; diff --git a/src/PityUnit.hxx b/src/PityUnit.hxx index 370579e..cccfe9b 100644 --- a/src/PityUnit.hxx +++ b/src/PityUnit.hxx @@ -19,77 +19,82 @@ namespace pEp { namespace PityTest11 { + // static template std::string PityUnit::_global_root_dir = "./pitytest_data/"; + // static template bool PityUnit::debug_log_enabled = false; + // static template - int PityUnit::procNodesCount = 0; + int PityUnit::procUnitsCount = 0; - // CONCSTRUCTORS + // CONSTRUCTOR template PityUnit::PityUnit( - PityUnit* const parent, + PityUnit* const parent, const std::string& name, - const NodeFunc test_func, + const std::function test_func, T* model, ExecutionMode exec_mode) : - _parent(parent), - _model(model), _name(_normalizeName(name)), _test_func(test_func), _exec_mode(exec_mode) + _parent{ parent }, + _model{ model }, _name{ _normalizeName(name) }, _test_func{ test_func }, _exec_mode{ + exec_mode + } { - logger_debug.set_instancename(getNodePath()); - if (!_isRootNode()) { - parent->_addChildNode(*this); + logger_debug.set_instancename(getPath()); + if (!_isRootUnit()) { + parent->_addChildUnit(*this); // Inherit - procNodeNr = _parent->procNodeNr; - //Or update if procNode - if (_isProcessNode()) { - procNodesCount++; - procNodeNr = procNodesCount; + procUnitNr = _parent->procUnitNr; + //Or update if procUnit + if (_isProcessUnit()) { + procUnitsCount++; + procUnitNr = procUnitsCount; } } else { - procNodeNr = procNodesCount; + procUnitNr = procUnitsCount; } } template - std::string PityUnit::getNodeName() const + std::string PityUnit::getName() const { return _name; } template - std::string PityUnit::getNodePath() const + std::string PityUnit::getPath() const { std::string ret; - if (!_isRootNode()) { - ret = _parent->getNodePath() + "/" + getNodeName(); + if (!_isRootUnit()) { + ret = _parent->getPath() + "/" + getName(); } else { - ret = getNodeName(); + ret = getName(); } return ret; } // For: - // RootNode - "" - // ProcessNode - ".../" + // RootUnit - "" + // ProcessUnit - ".../" // When Process as dir. parent - "...//name" // When no process as dir. parent - "...//.../name" template - std::string PityUnit::getNodePathShort() const + std::string PityUnit::getPathShort() const { std::string ret; - if (_isRootNode()) { - ret = getNodeName(); + if (_isRootUnit()) { + ret = getName(); } else { - if (_isProcessNode()) { - ret += ".../" + getNodeName(); + if (_isProcessUnit()) { + ret += ".../" + getName(); } else { - if (&(_parentingProcessNode()) == (_parent)) { - ret = _parentingProcessNode().getNodePathShort() + "/" + getNodeName(); + if (&(_parentingProcessUnit()) == (_parent)) { + ret = _parentingProcessUnit().getPathShort() + "/" + getName(); } else { - ret = _parentingProcessNode().getNodePathShort() + "/.../" + getNodeName(); + ret = _parentingProcessUnit().getPathShort() + "/.../" + getName(); } } } @@ -106,30 +111,30 @@ namespace pEp { if (_model != nullptr) { ret = _model; } else { - if (!_isRootNode()) { + if (!_isRootUnit()) { ret = _parent->getModel(); } } return ret; } - // Every RootNode has its own dir + // Every RootUnit has its own dir template - std::string PityUnit::rootNodeDir() const + std::string PityUnit::_rootUnitDir() const { - return getGlobalRootDir() + _rootNode().getNodeName() + "/"; + return getGlobalRootDir() + _rootUnit().getName() + "/"; } - // Every process has its own dir inside its rootNodeDir - // All other nodes inherit processDir from their Root/ProcessNode + // Every process has its own dir inside its rootUnitDir + // All other units inherit processDir from their Root/ProcessUnit template std::string PityUnit::processDir() const { - if (_isRootNode()) { - return rootNodeDir(); + if (_isRootUnit()) { + return _rootUnitDir(); } else { - if (_isProcessNode()) { - return rootNodeDir() + getNodeName() + "/"; + if (_isProcessUnit()) { + return _rootUnitDir() + getName() + "/"; } else { return _parent->processDir(); } @@ -143,10 +148,10 @@ namespace pEp { // } // static - template<> - void PityUnit::setGlobalRootDir(const std::string& dir) + template + void PityUnit::setGlobalRootDir(const std::string& dir) { - PityUnit::_global_root_dir = dir; + PityUnit::_global_root_dir = dir; } // static @@ -156,47 +161,23 @@ namespace pEp { return PityUnit::_global_root_dir; } - template - void PityUnit::_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&> child : _children) { - _recreateDir(child.second.processDir()); - } - } - logH3("INIT DONE"); - } - template void PityUnit::run() const { pEpLogClass("called"); // caller is never nullptr if called by another PityUnit - if (_isRootNode()) { + if (_isRootUnit()) { _init(); } // Execute in fork and wait here until process ends if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL) { // fork - logH2(_status_string("RUNNING")); - _executeInFork(std::bind(&PityUnit::_run, this), true); + _executeInFork(std::bind(&PityUnit::_run, this), true); // Execute in fork and go on, wait for process execution in the end } else if (_exec_mode == ExecutionMode::PROCESS_PARALLEL) { - logH2(_status_string("RUNNING")); - _executeInFork(std::bind(&PityUnit::_run, this), false); + _executeInFork(std::bind(&PityUnit::_run, this), false); // Execute as normal funciton } else if (_exec_mode == ExecutionMode::FUNCTION) { - logH3(_status_string("RUNNING")); _run(); } else if (_exec_mode == ExecutionMode::THREAD_PARALLEL) { 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"); } - if (_isRootNode()) { + if (_isRootUnit()) { _waitChildProcesses(); } } @@ -216,7 +197,7 @@ namespace pEp { std::stringstream builder; builder << std::string(indent * 4, ' '); - builder << getNodeName(); + builder << getName(); builder << " [ "; builder << to_string(_exec_mode) << " - "; builder << "\"" << processDir() << "\""; @@ -225,11 +206,13 @@ namespace pEp { ret = builder.str(); if (recursive) { - indent++; - for (const std::pair child : _children) { - ret += child.second.to_string(true, indent); + if (!_children.empty()) { + indent++; + for (const std::pair&> child : _children) { + ret += child.second.to_string(true, indent); + } + indent--; } - indent--; } return ret; } @@ -262,43 +245,60 @@ namespace pEp { builder << "["; builder << std::to_string(getpid()); builder << " - "; - builder << getNodePathShort(); + builder << getPathShort(); builder << "] - "; builder << msg; logRaw(builder.str()); } - template - void PityUnit::logRaw(const std::string& msg) const - { - Adapter::pEpLog::log(msg, termColor()); - } template void PityUnit::logH1(const std::string& msg) const { - Adapter::pEpLog::logH1(msg, termColor()); + Adapter::pEpLog::logH1(msg, _termColor()); } template void PityUnit::logH2(const std::string& msg) const { - Adapter::pEpLog::logH2(msg, termColor()); + Adapter::pEpLog::logH2(msg, _termColor()); } template void PityUnit::logH3(const std::string& msg) const { - Adapter::pEpLog::logH3(msg, termColor()); + Adapter::pEpLog::logH3(msg, _termColor()); } // PRIVATE --------------------------------------------------------------------------------- + template + void PityUnit::_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&> child : _children) { + // _recreateDir(child.second.processDir()); + // } + // } + logH3("INIT DONE"); + } + template void PityUnit::_run() const { + logH2(_status_string("STARTING")); _runSelf(); _runChildren(); } @@ -309,10 +309,11 @@ namespace pEp { if (_test_func != nullptr) { try { _test_func(*this); - logH3(_status_string("SUCCESS")); + logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_termColor()))); } catch (const std::exception& e) { logRaw("reason: " + std::string(e.what())); - logH3(_status_string("FAILED")); + logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_termColor()) + )); } } else { logRaw("No function to execute"); @@ -323,7 +324,7 @@ namespace pEp { void PityUnit::_runChildren() const { if (!_children.empty()) { - for (const std::pair child : _children) { + for (const std::pair&> child : _children) { child.second.run(); } } @@ -358,13 +359,13 @@ namespace pEp { } template - void PityUnit::_addChildNode(PityUnit& node) + void PityUnit::_addChildUnit(PityUnit& unit) { - _children.insert(std::pair(node.getNodeName(), node)); + _children.insert(std::pair&>(unit.getName(), unit)); } template - bool PityUnit::_isProcessNode() const + bool PityUnit::_isProcessUnit() const { bool ret = false; if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL || @@ -375,7 +376,7 @@ namespace pEp { } template - bool PityUnit::_isRootNode() const + bool PityUnit::_isRootUnit() const { if (_parent == nullptr) { return true; @@ -385,27 +386,27 @@ namespace pEp { } template - const PityUnit& PityUnit::_rootNode() const + const PityUnit& PityUnit::_rootUnit() const { - const PityUnit* ret = nullptr; - if (!_isRootNode()) { - ret = &(_parent->_rootNode()); + const PityUnit* ret = nullptr; + if (!_isRootUnit()) { + ret = &(_parent->_rootUnit()); } else { ret = this; } assert(ret != nullptr); - // cant be null because for createChildNode() you need to provide a TestNode& and - // the only other way is using createRootNode() which has parent == nullptr + // cant be null because for createChildUnit() you need to provide a TestUnit& and + // the only other way is using createRootUnit() which has parent == nullptr return *ret; } template - const PityUnit& PityUnit::_parentingProcessNode() const + const PityUnit& PityUnit::_parentingProcessUnit() const { - if (_isRootNode() || _isProcessNode()) { + if (_isRootUnit() || _isProcessUnit()) { return *this; } else { - return _parent->_parentingProcessNode(); + return _parent->_parentingProcessUnit(); } } @@ -427,15 +428,15 @@ namespace pEp { { std::string ret; ret = "[ " + to_string(_exec_mode) + ":" + std::to_string(getpid()) + " ] [ " + - getNodePathShort() + " ] [ " + msg + " ]"; + getPathShort() + " ] [ " + msg + " ]"; return ret; } template - Utils::Color PityUnit::colForProcNodeNr(int procNodeNr) const + Utils::Color PityUnit::_colForProcUnitNr(int procUnitNr) const { - switch (procNodeNr) { + switch (procUnitNr) { case 0: return Utils::Color::WHITE; case 1: @@ -456,9 +457,15 @@ namespace pEp { } template - Utils::Color PityUnit::termColor() const + Utils::Color PityUnit::_termColor() const { - return colForProcNodeNr(procNodeNr); + return _colForProcUnitNr(procUnitNr); + } + + template + void PityUnit::logRaw(const std::string& msg) const + { + Adapter::pEpLog::log(msg, _termColor()); } template @@ -485,6 +492,16 @@ namespace pEp { Utils::dir_create(path); } + template + void PityUnit::recreateDirsRecursively() const + { + _recreateDir(processDir()); + if (!_children.empty()) { + for (const std::pair&> child : _children) { + child.second.recreateDirsRecursively(); + } + } + } } // namespace PityTest11 } // namespace pEp