Solve legacy problem with clear operation (coredump with running threads)
[anna.git] / include / anna / testing / TestStep.hpp
index 782ce55..844b6be 100644 (file)
 #include <string>
 #include <vector>
 #include <thread>
+#include <atomic>
 
 // Project
 #include <anna/core/DataBlock.hpp>
-#include <anna/testing/TestCondition.hpp>
+#include <anna/testing/TestDiameterCondition.hpp>
 #include <anna/core/util/Millisecond.hpp>
 
 
@@ -57,7 +58,7 @@ class TestStep {
     void initialize(TestCase *testCase);
 
   public:
-    struct Type { enum _v { Unconfigured, Timeout, Sendxml2e, Sendxml2c, Delay, Wait, Cmd }; };
+    struct Type { enum _v { Unconfigured, Timeout, Sendxml2e, Sendxml2c, Delay, Wait, Cmd, IpLimit }; };
     static const char* asText(const Type::_v type) throw();
 
     TestStep(TestCase *testCase) : a_message(true), a_messageCodec(NULL), a_executed(false) { initialize(testCase); }
@@ -67,6 +68,7 @@ class TestStep {
     const Type::_v &getType() const throw() { return a_type; }
     const int &getNumber() const throw() { return a_number; }
     bool isCompleted() const throw() { return a_completed; }
+    anna::Millisecond getLapseMs() const throw() { return a_endTimestamp - a_beginTimestamp; }
 
     bool execute() throw();
     void complete() throw();
@@ -101,6 +103,7 @@ class TestStepTimeout : public TestStep {
 
   public:
     TestStepTimeout(TestCase *testCase) : TestStep(testCase), a_timeout(0), a_timer(NULL) { a_type = Type::Timeout; }
+    ~TestStepTimeout() { do_reset(); }
 
     // setter & getters
     void setTimeout(const anna::Millisecond &t) throw() { a_timeout = t; }
@@ -110,11 +113,12 @@ class TestStepTimeout : public TestStep {
     bool do_execute() throw();
     void do_complete() throw(); // timeout reached, test case failed
     void do_reset() throw();
+    void cancelTimer() throw();
     anna::xml::Node* asXML(anna::xml::Node* parent) throw();
 };
 
 
-class TestStepSendxml : public TestStep {
+class TestStepSendDiameterXml : public TestStep {
 
   protected:
     // possible end points:
@@ -127,11 +131,11 @@ class TestStepSendxml : public TestStep {
     bool a_expired; // a_endTimestamp will be the expiration reception timestamp
 
   public:
-    TestStepSendxml(TestCase *testCase) : TestStep(testCase),
+    TestStepSendDiameterXml(TestCase *testCase) : TestStep(testCase),
       a_expired(false),
       a_originHost(NULL),
       a_waitForRequestStepNumber(-1) {;}
-    ~TestStepSendxml() {;}
+    ~TestStepSendDiameterXml() { do_reset(); }
 
     // setter & getters
     void setOriginHost(anna::diameter::comm::OriginHost *host) throw() { a_originHost = host; }
@@ -148,14 +152,14 @@ class TestStepSendxml : public TestStep {
     anna::xml::Node* asXML(anna::xml::Node* parent) throw();
 };
 
-class TestStepSendxml2e : public TestStepSendxml {
+class TestStepSendDiameterXml2e : public TestStepSendDiameterXml {
   public:
-    TestStepSendxml2e(TestCase *testCase) : TestStepSendxml(testCase) { a_type = Type::Sendxml2e; }
+    TestStepSendDiameterXml2e(TestCase *testCase) : TestStepSendDiameterXml(testCase) { a_type = Type::Sendxml2e; }
 };
 
-class TestStepSendxml2c : public TestStepSendxml {
+class TestStepSendDiameterXml2c : public TestStepSendDiameterXml {
   public:
-    TestStepSendxml2c(TestCase *testCase) : TestStepSendxml(testCase) { a_type = Type::Sendxml2c; }
+    TestStepSendDiameterXml2c(TestCase *testCase) : TestStepSendDiameterXml(testCase) { a_type = Type::Sendxml2c; }
 };
 
 
@@ -165,6 +169,7 @@ class TestStepDelay : public TestStep {
 
   public:
     TestStepDelay(TestCase *testCase) : TestStep(testCase), a_delay(0), a_timer(NULL) { a_type = Type::Delay; }
+    ~TestStepDelay() { do_reset(); }
 
     // setter & getters
     void setDelay(const anna::Millisecond &d) throw() { a_delay = d; }
@@ -174,23 +179,24 @@ class TestStepDelay : public TestStep {
     bool do_execute() throw();
     void do_complete() throw(); // delay reached
     void do_reset() throw();
+    void cancelTimer() throw();
     anna::xml::Node* asXML(anna::xml::Node* parent) throw();
 };
 
 
-class TestStepWait : public TestStep {
+class TestStepWaitDiameter : public TestStep {
 
-    TestCondition a_condition;
+    TestDiameterCondition a_condition;
     anna::diameter::comm::ClientSession *a_clientSession;
     anna::diameter::comm::ServerSession *a_serverSession;
 
   public:
-    TestStepWait(TestCase *testCase) : TestStep(testCase) {
+    TestStepWaitDiameter(TestCase *testCase) : TestStep(testCase) {
       a_type = Type::Wait;
       a_clientSession = NULL;
       a_serverSession = NULL;
     }
-    ~TestStepWait() {;}
+    ~TestStepWaitDiameter() { do_reset(); }
 
     // setter & getters
     void setCondition(bool fromEntity,
@@ -205,7 +211,7 @@ class TestStepWait : public TestStep {
     anna::diameter::comm::ClientSession *getClientSession() const throw() { return a_clientSession; }
     anna::diameter::comm::ServerSession *getServerSession() const throw() { return a_serverSession; }
 
-    const TestCondition &getCondition() const throw() { return a_condition; }
+    const TestDiameterCondition &getCondition() const throw() { return a_condition; }
     //void setMsgDataBlock(const anna::DataBlock &db) throw() { a_message = db; }
     bool fulfilled(const anna::DataBlock &db/*, bool matchSessionId = true*/) throw();
     const anna::DataBlock &getMsgDataBlock() const throw() { return a_message; }
@@ -223,7 +229,7 @@ class TestStepCmd : public TestStep {
 
   std::string a_script;
   std::thread a_thread;
-  bool a_threadRunning;
+  std::atomic<bool> a_threadRunning;
   bool a_threadDeprecated;
   int a_resultCode;
   std::string a_errorMsg;
@@ -233,9 +239,13 @@ class TestStepCmd : public TestStep {
 
   public:
     TestStepCmd(TestCase *testCase) : TestStep(testCase), a_threadRunning(false), a_threadDeprecated(false), a_resultCode(-2)/*, a_output("")*/, a_errorMsg(""), a_childPid(-1) { a_type = Type::Cmd; }
+    ~TestStepCmd() { do_reset(); }
 
     // setter & getters
-    void setThreadRunning(bool running) throw() { a_threadRunning = running; }
+    void setThreadRunning() throw() { a_threadRunning = true; }
+    void setThreadNotRunning() throw() { a_threadRunning = false; }
+    bool threadRunning() const throw() { return a_threadRunning; }
+    bool threadNotRunning() const throw() { return !a_threadRunning; }
 
     void setResultCode(int rc) throw() { a_resultCode = rc; }
     int getResultCode() const throw() { return a_resultCode; }
@@ -256,6 +266,26 @@ class TestStepCmd : public TestStep {
     anna::xml::Node* asXML(anna::xml::Node* parent) throw();
 };
 
+
+class TestStepIpLimit : public TestStep {
+
+    unsigned int a_ipLimit;
+
+  public:
+    TestStepIpLimit(TestCase *testCase) : TestStep(testCase), a_ipLimit(1) { a_type = Type::IpLimit; }
+    ~TestStepIpLimit() { do_reset(); }
+
+    // setter & getters
+    void setIpLimit(unsigned int limit) throw() { a_ipLimit = limit; }
+    unsigned int getIpLimit() const throw() { return a_ipLimit; }
+
+    // virtuals
+    bool do_execute() throw();
+    void do_complete() throw();
+    void do_reset() throw() {;}
+    anna::xml::Node* asXML(anna::xml::Node* parent) throw();
+};
+
 }
 }