Browse Source

Tests: AbstractPityUnit - move init from constructor to run()

LIB-11
heck 11 months ago
parent
commit
bbe6b5b4c3
3 changed files with 93 additions and 80 deletions
  1. +83
    -67
      test/pitytest11/src/AbstractPityUnit.cc
  2. +8
    -11
      test/pitytest11/src/AbstractPityUnit.hh
  3. +2
    -2
      test/pitytest11/src/PityUnit.hxx

+ 83
- 67
test/pitytest11/src/AbstractPityUnit.cc View File

@ -26,10 +26,8 @@ namespace pEp {
int AbstractPityUnit::procUnitsCount = 0;
AbstractPityUnit::AbstractPityUnit(const std::string &name, ExecutionMode exec_mode) :
PityTree<AbstractPityUnit>(*this, name),
_exec_mode{ exec_mode }
PityTree<AbstractPityUnit>(*this, name), _exec_mode{ exec_mode }, procUnitNr{ 0 }
{
_init();
}
AbstractPityUnit::AbstractPityUnit(
@ -37,27 +35,11 @@ namespace pEp {
const std::string &name,
ExecutionMode exec_mode) :
PityTree<AbstractPityUnit>(*this, name, parent),
_exec_mode{ exec_mode }
_exec_mode{ exec_mode }, procUnitNr{ 0 }
{
_init();
}
void AbstractPityUnit::_init()
{
logger_debug.set_instancename(getPath());
if (!isRoot()) {
// Inherit
procUnitNr = getParent()->procUnitNr;
//Or update if procUnit
if (_isProcessUnit()) {
_createTransport();
procUnitsCount++;
procUnitNr = procUnitsCount;
}
} else {
procUnitNr = procUnitsCount;
}
}
// static
void AbstractPityUnit::setGlobalRootDir(const std::string &dir)
{
@ -131,34 +113,97 @@ namespace pEp {
}
}
void AbstractPityUnit::run()
void AbstractPityUnit::_initProcUnitNrRecurse()
{
if (!isRoot()) {
// Inherit
procUnitNr = getParent()->procUnitNr;
//Or update if procUnit
if (_isProcessUnit()) {
procUnitsCount++;
procUnitNr = procUnitsCount;
}
} else {
procUnitNr = procUnitsCount;
}
// Recurse
for (const auto &chld : getChildren()) {
chld.second._initProcUnitNrRecurse();
}
}
void AbstractPityUnit::_initTransportRecurse()
{
logger_debug.set_instancename(getPath());
if (!isRoot()) {
if (_isProcessUnit()) {
_createTransport();
}
}
// Recurse
for (const auto &chld : getChildren()) {
chld.second._initTransportRecurse();
}
}
void AbstractPityUnit::_initDirsRecursive()
{
Utils::dir_recreate(processDir());
// Recurse
for (const auto &child : getChildren()) {
child.second._initDirsRecursive();
}
}
void AbstractPityUnit::run(bool init_tree)
{
pEpLogClass("called");
_log_mutex = std::make_shared<fs_mutex>("log.mutex");
_log_mutex->release();
setenv("HOME", processDir().c_str(), true);
if (isRoot()) {
_initrun();
if (init_tree) {
logH1("PityTest Starting...");
_logRaw("RootUnit: " + getPathShort());
_logRaw("GlobalRootDir: " + getGlobalRootDir());
_logRaw("Ensuring GlobalRootDir...");
Utils::dir_ensure(getGlobalRootDir());
_logRaw("Determining process numbers recursively...");
_initProcUnitNrRecurse();
_logRaw("Initializing Transport recursively...");
_initTransportRecurse();
_logRaw("Recreating process dirs recursively...");
_initDirsRecursive();
_logRaw("\n\nTestTree");
_logRaw("--------");
_logRaw(to_string() + "\n");
}
// TODO: hack
setenv("HOME", processDir().c_str(), true);
// Execute in fork and wait here until process ends
if (_exec_mode == ExecutionMode::PROCESS_SEQUENTIAL) { // fork
_executeInFork(std::bind(&AbstractPityUnit::_run, this), true);
_executeInFork(std::bind(&AbstractPityUnit::_runRecurse, this), true);
// Execute in fork and go on, wait for process execution in the end
} else if (_exec_mode == ExecutionMode::PROCESS_PARALLEL) {
_executeInFork(std::bind(&AbstractPityUnit::_run, this), false);
_executeInFork(std::bind(&AbstractPityUnit::_runRecurse, this), false);
// Execute as normal function
} else if (_exec_mode == ExecutionMode::FUNCTION) {
_run();
_runRecurse();
} else if (_exec_mode == ExecutionMode::THREAD_PARALLEL) {
throw std::invalid_argument(to_string(_exec_mode) + " - not implemented");
} else if (_exec_mode == ExecutionMode::THREAD_SEQUENTIAL) {
throw std::invalid_argument(to_string(_exec_mode) + " - not implemented");
}
if (isRoot()) {
if (init_tree) {
_waitChildProcesses();
}
}
@ -209,15 +254,6 @@ namespace pEp {
}
}
void AbstractPityUnit::recreateDirsRecursively()
{
Utils::dir_recreate(processDir());
if (!getChildren().empty()) {
for (const auto child : getChildren()) {
child.second.recreateDirsRecursively();
}
}
}
void AbstractPityUnit::registerAsTransportEndpoint()
{
@ -248,47 +284,27 @@ namespace pEp {
void AbstractPityUnit::logH1(const std::string &msg) const
{
Adapter::pEpLog::logH1(msg, _termColor());
Adapter::pEpLog::logH1(msg, _color());
}
void AbstractPityUnit::logH2(const std::string &msg) const
{
Adapter::pEpLog::logH2(msg, _termColor());
Adapter::pEpLog::logH2(msg, _color());
}
void AbstractPityUnit::logH3(const std::string &msg) const
{
Adapter::pEpLog::logH3(msg, _termColor());
Adapter::pEpLog::logH3(msg, _color());
}
// PRIVATE ---------------------------------------------------------------------------------
void AbstractPityUnit::_initrun()
{
logH1("PityTest Starting...");
_logRaw("RootUnit: " + getPathShort());
_logRaw("GlobalRootDir: " + getGlobalRootDir());
_logRaw("\nTestTree");
_logRaw("--------");
_logRaw(to_string());
logH3("INIT");
Utils::dir_ensure(getGlobalRootDir());
recreateDirsRecursively();
logH3("INIT DONE");
}
void AbstractPityUnit::_run()
void AbstractPityUnit::_runRecurse()
{
logH2(_status_string("STARTING"));
_runSelf();
_runChildren();
}
void AbstractPityUnit::_runChildren() const
{
if (!getChildren().empty()) {
for (const auto child : getChildren()) {
child.second.run();
child.second.run(false);
}
}
}
@ -322,7 +338,7 @@ namespace pEp {
logH3(
color + "PROCESS [ " + std::to_string((int)pid) +
" ] EXITED with status code: " + std::to_string(status) +
Utils::to_termcol(_termColor()));
Utils::to_termcol(_color()));
}
}
@ -402,7 +418,7 @@ namespace pEp {
}
}
Utils::Color AbstractPityUnit::_termColor() const
Utils::Color AbstractPityUnit::_color() const
{
return _colForProcUnitNr(procUnitNr);
}
@ -411,7 +427,7 @@ namespace pEp {
{
// fs-mutex to sync across processes
_log_mutex->aquire();
Adapter::pEpLog::log(msg, _termColor());
Adapter::pEpLog::log(msg, _color());
_log_mutex->release();
}


+ 8
- 11
test/pitytest11/src/AbstractPityUnit.hh View File

@ -31,8 +31,8 @@ namespace pEp {
INHERIT
};
AbstractPityUnit(const std::string& name, ExecutionMode exec_mode = ExecutionMode::FUNCTION);
AbstractPityUnit(
explicit AbstractPityUnit(const std::string& name, ExecutionMode exec_mode = ExecutionMode::FUNCTION);
explicit AbstractPityUnit(
AbstractPityUnit& parent,
const std::string& name,
ExecutionMode exec_mode = ExecutionMode::FUNCTION);
@ -47,7 +47,7 @@ namespace pEp {
std::string transportDir();
// Main funcs
void run();
void run(bool init_tree = true);
std::string to_string(bool recursive = true, int indent = 0);
static std::string to_string(const ExecutionMode& emode);
@ -58,9 +58,6 @@ namespace pEp {
void logH2(const std::string& msg) const;
void logH3(const std::string& msg) const;
// Util
void recreateDirsRecursively();
//Transport
PityTransport* transport() const;
void registerAsTransportEndpoint();
@ -73,7 +70,7 @@ namespace pEp {
protected:
std::string _status_string(const std::string& msg) const;
static Utils::Color _colForProcUnitNr(int procUnitNr);
Utils::Color _termColor() const;
Utils::Color _color() const;
void _logRaw(const std::string& msg) const;
// internal logging
@ -82,11 +79,11 @@ namespace pEp {
private:
// METHODS
// Execution
void _init();
void _initrun();
void _run();
void _initProcUnitNrRecurse();
void _initTransportRecurse();
void _initDirsRecursive();
void _runRecurse();
virtual void _runSelf() = 0;
void _runChildren() const;
void _executeInFork(std::function<void(void)> func, bool wait_child) const;
void _waitChildProcesses() const;


+ 2
- 2
test/pitytest11/src/PityUnit.hxx View File

@ -50,10 +50,10 @@ namespace pEp {
if (_test_func != nullptr) {
try {
_test_func(*this, getPerspective());
logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_termColor())));
logH3(_status_string("\033[1m\033[32mSUCCESS" + Utils::to_termcol(_color())));
} catch (const std::exception &e) {
_logRaw("reason: " + std::string(e.what()));
logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_termColor())));
logH3(_status_string("\033[1m\033[31mFAILED" + Utils::to_termcol(_color())));
}
} else {
_logRaw("No function to execute");


Loading…
Cancel
Save