Solve legacy problem with clear operation (coredump with running threads)
[anna.git] / source / testing / TestCase.cpp
index fd4ed42..8c38d06 100644 (file)
@@ -85,11 +85,13 @@ TestCase::TestCase(unsigned int id, const std::string &description) :
 }
 
 TestCase::~TestCase() {
+  // TestCase should not be deleted until is safeToClear()
   reset(true); // hard reset
   std::vector<TestStep*>::const_iterator it;
   for (it = a_steps.begin(); it != a_steps.end(); it++) delete (*it);
 }
 
+
 const char* TestCase::asText(const State::_v state)
 throw() {
   static const char* text [] = { "Initialized", "InProgress", "Failed", "Success" };
@@ -289,6 +291,21 @@ bool TestCase::reset(bool hard) throw() {
   return true;
 }
 
+bool TestCase::safeToClear() {
+
+  // Check if any step is running (command steps):
+  std::vector<TestStep*>::const_iterator it;
+  for (it = a_steps.begin(); it != a_steps.end(); it++) {
+    if ((*it)->getType() == TestStep::Type::Cmd) {
+      const TestStepCmd *tscmd = static_cast<const TestStepCmd*>(*it);
+      if(tscmd->threadRunning())
+        return false;
+    }
+  }
+
+  return true;
+}
+
 void TestCase::assertInitialized() const throw(anna::RuntimeException) {
   if (isFinished())
     throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu (%s) has finished. You must reset it to append new steps (or do it during execution, which is also allowed).", a_id, a_description.c_str()), ANNA_FILE_LOCATION);