+////////////////////////////////////////////////////////////////////////////////////////////////////////
+// TestStepCmd
+////////////////////////////////////////////////////////////////////////////////////////////////////////
+anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent)
+throw() {
+ anna::xml::Node* result = TestStep::asXML(parent);
+ //parent->createChild("TestStepCmd");
+
+ result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
+ if (a_errorMsg != "") result->createAttribute("ErrorMessage", a_errorMsg);
+ if (a_threadRunning) {
+ if (a_childPid != -1)
+ result->createAttribute("ChildPid", a_childPid);
+ }
+ else {
+ if (a_resultCode != -2) {
+ result->createAttribute("ResultCode", a_resultCode);
+ //if (a_output != "") result->createAttribute("Output", a_output);
+ }
+ }
+
+ return result;
+}
+
+bool TestStepCmd::do_execute() throw() {
+ if (!a_threadRunning /* || a_threadDeprecated DO NOT WANT TO OVERLAP ... */) {
+ // Special tags to replace:
+ std::string cmd = getScript();
+ size_t index;
+ while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID)) != std::string::npos)
+ cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID), anna::functions::asString(TestManager::instantiate().getPoolCycle()));
+ while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID)) != std::string::npos)
+ cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID), anna::functions::asString(a_testCase->getId()));
+ while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID)) != std::string::npos)
+ cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID), anna::functions::asString(getNumber()));
+
+ a_thread = std::thread(cmdRunOnThread, this, cmd);
+ //a_thread = std::thread(cmdRunOnThreadWithFork, this, cmd);
+
+ a_thread.detach();
+ }
+
+ return false; // don't go next (wait complete): If system function on thread stucks, then the reset test case will stuck here forever.
+ // We must implement a interrupt procedure for the thread on reset call... TODO !
+}
+
+void TestStepCmd::do_complete() throw() {
+
+ a_threadRunning = false;
+ if (a_threadDeprecated) {
+ a_threadDeprecated = false;
+ do_reset();
+ setErrorMsg(anna::functions::asString("Step %d deprecated due to previous reset for Test Case %llu", getNumber(), a_testCase->getId()));
+ a_testCase->setState(TestCase::State::Failed);
+ return; // ignore TODO: interrupt the thread to avoid execution of the script
+ }
+
+ if (getResultCode() != 0)
+ a_testCase->setState(TestCase::State::Failed);
+ else
+ next(); // next() invoked here because execute() is always false for delay and never advance the iterator
+ // TODO, avoid this recursion
+}
+
+void TestStepCmd::do_reset() throw() {
+
+ if (a_threadRunning) {
+ std::string s_warn = anna::functions::asString("Thread still in progress: deprecating step %d for Test Case %llu", getNumber(), a_testCase->getId());
+ LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
+ a_threadDeprecated = true;
+ }
+// if (a_threadRunning) {
+// std::string s_warn = anna::functions::asString("Thread still in progress: killing child pid %d within step %d for Test Case %llu", a_childPid, getNumber(), a_testCase->getId());
+// LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
+// kill (a_childPid, SIGKILL);
+// }
+
+ a_resultCode = -2;
+ a_errorMsg = "";
+ //a_output = "";
+ a_childPid = -1;
+}
+