X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=source%2Ftesting%2FTestCase.cpp;h=991a254887e1cfa2fce2ee9a462eeace6901a5f3;hb=5a6cba5fde2b2f538a7515f8293cc0a8d9589dfa;hp=d048dfc2284d47334001f7e1d8154816d5850196;hpb=bfedf5aed2670ccd65405c0ff96539ccb88b8f23;p=anna.git diff --git a/source/testing/TestCase.cpp b/source/testing/TestCase.cpp index d048dfc..991a254 100644 --- a/source/testing/TestCase.cpp +++ b/source/testing/TestCase.cpp @@ -33,18 +33,18 @@ using namespace anna::testing; /////////////////////////////////////////////////////////////////////////////////////////////////// -void TestCase::DebugSummary::addHint(const std::string &hint) throw() { +void TestCase::DebugSummary::addHint(const std::string &hint) { event_t event; event.Timestamp = anna::functions::millisecond(); event.Hint = hint; a_events.push_back(event); } -void TestCase::DebugSummary::clear() throw() { +void TestCase::DebugSummary::clear() { a_events.clear(); } -anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const throw() { +anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const { anna::xml::Node* result = parent->createChild("DebugSummary"); std::vector::const_iterator it; @@ -56,13 +56,27 @@ anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const th return result; }; + +std::string TestCase::DebugSummary::asString() const { + std::string result = ""; + + std::vector::const_iterator it; + for (it = a_events.begin(); it != a_events.end(); it++) { + result += anna::functions::asString("[Timestamp: %s] %s", (*it).Timestamp.asString().c_str(), (*it).Hint.c_str()); + } + + return result; +}; + /////////////////////////////////////////////////////////////////////////////////////////////////// -TestCase::TestCase(unsigned int id) : +TestCase::TestCase(unsigned int id, const std::string &description) : a_id(id), + a_description((description != "") ? description : (anna::functions::asString("Testcase_%d", id))), a_state(State::Initialized), - a_startTime(0), + a_startTimestamp(0), + a_finishTimestamp(0), a_interactiveAmount(-1) { /*a_stepsIt = a_steps.end()*/; @@ -71,24 +85,37 @@ TestCase::TestCase(unsigned int id) : } TestCase::~TestCase() { + // TestCase should not be deleted until is safeToClear() reset(true); // hard reset std::vector::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" }; return text [state]; } +anna::Millisecond TestCase::getLapseMs() const { + return ((a_finishTimestamp >= a_startTimestamp) ? (a_finishTimestamp - a_startTimestamp) : (anna::Millisecond)0); +} + anna::xml::Node* TestCase::asXML(anna::xml::Node* parent) const -throw() { +{ anna::xml::Node* result = parent->createChild("TestCase"); result->createAttribute("Id", a_id); + result->createAttribute("Description", a_description); result->createAttribute("State", asText(a_state)); - result->createAttribute("StartTimestamp", a_startTime.asString()); + result->createAttribute("StartTimestamp", a_startTimestamp.asString()); + + if (a_finishTimestamp != 0) { + result->createAttribute("FinishTimestamp", a_finishTimestamp.asString()); + result->createAttribute("LapseMs", getLapseMs()); + } + int steps = a_steps.size(); if (steps != 0) { result->createAttribute("NumberOfTestSteps", steps); @@ -107,24 +134,24 @@ throw() { return result; } -std::string TestCase::asXMLString() const throw() { +std::string TestCase::asXMLString() const { anna::xml::Node root("root"); return anna::xml::Compiler().apply(asXML(&root)); } -bool TestCase::hasSameCondition(const TestCondition &condition) const throw() { +bool TestCase::hasSameCondition(const TestDiameterCondition &condition) const { std::vector::const_iterator it; - TestStepWait *step; + TestStepWaitDiameter *step; for (it = a_steps.begin(); it != a_steps.end(); it++) { if ((*it)->getType() != TestStep::Type::Wait) continue; - step = (TestStepWait *)(*it); + step = (TestStepWaitDiameter *)(*it); if (step->getCondition() == condition) return true; } return false; } -void TestCase::setState(const State::_v &state) throw() { +void TestCase::setState(const State::_v &state) { State::_v previousState = a_state; if (state == previousState) return; @@ -136,6 +163,29 @@ void TestCase::setState(const State::_v &state) throw() { if (isFinished()) { + const char *literal = "FINISHED Test Case %llu/%llu [%s] => %s"; + TestManager& testManager (TestManager::instantiate ()); + LOGDEBUG(anna::Logger::debug(anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str(), asText(a_state)), ANNA_FILE_LOCATION)); + + if (testManager.getDumpStdout()) { + std::cout << std::endl << anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str(), asText(a_state)) << std::endl; + } + + a_finishTimestamp = anna::functions::millisecond(); + + // Cancel existing timers: + std::vector::iterator it; + for (it = a_steps.begin(); it != a_steps.end(); it++) { + if ((*it)->getType() == TestStep::Type::Timeout) { + TestStepTimeout *step = (TestStepTimeout *)(*it); + step->cancelTimer(); + } + else if ((*it)->getType() == TestStep::Type::Delay) { + TestStepDelay *step = (TestStepDelay *)(*it); + step->cancelTimer(); + } + } + if ((getState() == State::Failed) && (!testManager.getDumpFailedReports())) return; if ((getState() == State::Success) && (!testManager.getDumpSuccessReports())) return; // report file name: cycle-.testcase-.xml @@ -165,7 +215,7 @@ void TestCase::setState(const State::_v &state) throw() { } } -bool TestCase::done() throw() { +bool TestCase::done() { if (a_stepsIt == a_steps.end()) { setState(State::Success); return true; @@ -174,22 +224,30 @@ bool TestCase::done() throw() { return false; } -bool TestCase::process() throw() { +bool TestCase::process() { if (steps() == 0) { - LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu is empty, nothing to execute", a_id), ANNA_FILE_LOCATION)); + LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu (%s) is empty, nothing to execute", a_id, a_description.c_str()), ANNA_FILE_LOCATION)); return false; } if (isFinished()) { - LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu is finished, nothing done until soft-reset", a_id), ANNA_FILE_LOCATION)); + LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu (%s) is finished, nothing done until soft-reset", a_id, a_description.c_str()), ANNA_FILE_LOCATION)); return false; } + const char *literal = "PROCESS Test Case %llu/%llu [%s]"; + TestManager& testManager (TestManager::instantiate ()); + LOGDEBUG(anna::Logger::debug(anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str()), ANNA_FILE_LOCATION)); + if (a_state == State::Initialized) { + if (testManager.getDumpStdout()) { + std::cout << std::endl << std::endl << anna::functions::asString(literal, getId(), testManager.tests(), getDescription().c_str()) << std::endl; + } + a_stepsIt = a_steps.begin(); setState(State::InProgress); // For 'wait' steps (not really useful, but better than nothing: begin timestamp on test case start timestamp...): - a_startTime = anna::functions::millisecond(); + a_startTimestamp = anna::functions::millisecond(); } // Check end of the test case: @@ -206,7 +264,7 @@ bool TestCase::process() throw() { return somethingDone; } -bool TestCase::reset(bool hard) throw() { +bool TestCase::reset(bool hard) { // Soft reset if finished: if (!hard /* is soft reset */ && !isFinished()) return false; @@ -224,7 +282,8 @@ bool TestCase::reset(bool hard) throw() { (*it)->reset(); a_debugSummary.clear(); - a_startTime = 0; + a_startTimestamp = 0; + a_finishTimestamp = 0; a_interactiveAmount = -1; setState(State::Initialized); @@ -232,12 +291,27 @@ bool TestCase::reset(bool hard) throw() { return true; } -void TestCase::assertInitialized() const throw(anna::RuntimeException) { +bool TestCase::safeToClear() { + + // Check if any step is running (command steps): + std::vector::const_iterator it; + for (it = a_steps.begin(); it != a_steps.end(); it++) { + if ((*it)->getType() == TestStep::Type::Cmd) { + const TestStepCmd *tscmd = static_cast(*it); + if(tscmd->threadRunning()) + return false; + } + } + + return true; +} + +void TestCase::assertInitialized() const noexcept(false) { if (isFinished()) - throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu has finished. You must reset it to append new steps (or do it during execution, which is also allowed).", a_id), ANNA_FILE_LOCATION); + 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); } -void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) { +void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) noexcept(false) { bool isRequest = anna::diameter::codec::functions::isRequest(db); bool registerKeys = ((isRequest && toEntity) || (!isRequest && !toEntity) /* (*) */); @@ -250,13 +324,13 @@ void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(ann if (isRequest) { anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db); if (a_hopByHops.find(hbh) != a_hopByHops.end()) - throw anna::RuntimeException(anna::functions::asString("Another request has been programmed with the same hop-by-hop (%llu) in this test case (%llu)", hbh, a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Another request has been programmed with the same hop-by-hop (%llu) in this test case (%llu, '%s')", hbh, a_id, a_description.c_str()), ANNA_FILE_LOCATION); a_hopByHops[hbh] = NULL; // may be assigned to a wait condition } if (registerKeys) { TestManager &testManager = TestManager::instantiate(); - testManager.registerSessionId(anna::diameter::helpers::base::functions::getSessionId(db), this); + testManager.registerKey1(anna::diameter::helpers::base::functions::getSessionId(db), this); std::string subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(db, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164); @@ -264,143 +338,158 @@ void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(ann subscriberId = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(db, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI); if (subscriberId != "") - testManager.registerSubscriberId(subscriberId, this); + testManager.registerKey2(subscriberId, this); } } -void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException) { +void TestCase::addTimeout(const anna::Millisecond &timeout) noexcept(false) { assertInitialized(); TestStepTimeout *step = new TestStepTimeout(this); step->setTimeout(timeout); addStep(step); } -void TestCase::addSendxml2e(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException) { +void TestCase::addSendDiameterXml2e(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) noexcept(false) { assertInitialized(); assertMessage(db, true /* to entity */); if (stepNumber != -1) { const TestStep *stepReferred = getStep(stepNumber); if (!stepReferred) - throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); if (stepReferred->getType() != TestStep::Type::Wait) - throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); - const TestCondition &tc = (static_cast(stepReferred))->getCondition(); + const TestDiameterCondition &tc = (static_cast(stepReferred))->getCondition(); if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors - throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); } } - TestStepSendxml2e *step = new TestStepSendxml2e(this); + TestStepSendDiameterXml2e *step = new TestStepSendDiameterXml2e(this); step->setMsgDataBlock(db); step->setOriginHost(host); step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference addStep(step); } -void TestCase::addSendxml2c(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) throw(anna::RuntimeException) { +void TestCase::addSendDiameterXml2c(const anna::DataBlock &db, anna::diameter::comm::OriginHost *host, int stepNumber) noexcept(false) { assertInitialized(); assertMessage(db, false /* to client */); - TestStepSendxml2c *step = new TestStepSendxml2c(this); + if (stepNumber != -1) { + const TestStep *stepReferred = getStep(stepNumber); + if (!stepReferred) + throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); + + if (stepReferred->getType() != TestStep::Type::Wait) + throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); + + const TestDiameterCondition &tc = (static_cast(stepReferred))->getCondition(); + if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors + throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); + } + } + + TestStepSendDiameterXml2c *step = new TestStepSendDiameterXml2c(this); step->setMsgDataBlock(db); step->setOriginHost(host); + step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference addStep(step); } -void TestCase::addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException) { +void TestCase::addDelay(const anna::Millisecond &delay) noexcept(false) { assertInitialized(); TestStepDelay *step = new TestStepDelay(this); step->setDelay(delay); addStep(step); } -void TestCase::addWait(bool fromEntity, +void TestCase::addWaitDiameter(bool fromEntity, const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId, const std::string &sessionId, const std::string &resultCode, - const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw(anna::RuntimeException) { + const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) noexcept(false) { assertInitialized(); std::string usedHopByHop = hopByHop; - TestStepWait *step = NULL; + TestStepWaitDiameter *step = NULL; // Check basic conditions: if (bitR == "1") { if (resultCode != "") - throw anna::RuntimeException(anna::functions::asString("You cannot specify Result-Code (%s) for a wait condition of a diameter request message (test case %llu)", resultCode.c_str(), a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("You cannot specify Result-Code (%s) for a wait condition of a diameter request message (test case %llu, '%s')", resultCode.c_str(), a_id, a_description.c_str()), ANNA_FILE_LOCATION); if (hopByHop != "") - throw anna::RuntimeException(anna::functions::asString("You cannot specify Hop-by-hop (%s) for a wait condition of a diameter request message (test case %llu)", hopByHop.c_str(), a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("You cannot specify Hop-by-hop (%s) for a wait condition of a diameter request message (test case %llu, '%s')", hopByHop.c_str(), a_id, a_description.c_str()), ANNA_FILE_LOCATION); } else { if (hopByHop != "") { if (hopByHop[0] == '#') { if (steps() == 0) - throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu)", a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu, '%s')", a_id, a_description.c_str()), ANNA_FILE_LOCATION); int stepNumber = atoi(hopByHop.substr(1).c_str()); const TestStep *stepReferred = getStep(stepNumber); if (!stepReferred) - throw anna::RuntimeException(anna::functions::asString("Step reference number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Step reference number (%d) do not exists (test case %llu, '%s')", stepNumber, a_id, a_description.c_str()), ANNA_FILE_LOCATION); if (stepReferred->getType() != TestStep::Type::Sendxml2e && stepReferred->getType() != TestStep::Type::Sendxml2c) - throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu)", a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu, '%s')", a_id, a_description.c_str()), ANNA_FILE_LOCATION); - const anna::DataBlock &db = (static_cast(stepReferred))->getMsgDataBlock(); + const anna::DataBlock &db = (static_cast(stepReferred))->getMsgDataBlock(); bool isAnswer = anna::diameter::codec::functions::isAnswer(db); if (isAnswer) - throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu)", a_id), ANNA_FILE_LOCATION); + throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu, '%s')", a_id, a_description.c_str()), ANNA_FILE_LOCATION); // Hop-by-hop: anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db); usedHopByHop = anna::functions::asString(hbh); - step = new TestStepWait(this); + step = new TestStepWaitDiameter(this); a_hopByHops[hbh /* always exists: is the info we calculated above */] = step; } } } - if (!step) step = new TestStepWait(this); + if (!step) step = new TestStepWaitDiameter(this); step->setCondition(fromEntity, code, bitR, usedHopByHop, applicationId, sessionId, resultCode, msisdn, imsi, serviceContextId); LOGINFORMATION( if (hasSameCondition(step->getCondition())) - anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION); + anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu, '%s'). Are you sure ?", a_id, a_description.c_str()), ANNA_FILE_LOCATION); ); addStep(step); } -void TestCase::addWaitRegexpHex(bool fromEntity, const std::string ®exp) throw(anna::RuntimeException) { +void TestCase::addWaitDiameterRegexpHex(bool fromEntity, const std::string ®exp) noexcept(false) { assertInitialized(); - TestStepWait *step = new TestStepWait(this); + TestStepWaitDiameter *step = new TestStepWaitDiameter(this); step->setConditionRegexpHex(fromEntity, regexp); LOGINFORMATION( if (hasSameCondition(step->getCondition())) - anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION); + anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu, '%s'). Are you sure ?", a_id, a_description.c_str()), ANNA_FILE_LOCATION); ); addStep(step); } -void TestCase::addWaitRegexpXml(bool fromEntity, const std::string ®exp) throw(anna::RuntimeException) { +void TestCase::addWaitDiameterRegexpXml(bool fromEntity, const std::string ®exp) noexcept(false) { assertInitialized(); - TestStepWait *step = new TestStepWait(this); + TestStepWaitDiameter *step = new TestStepWaitDiameter(this); step->setConditionRegexpXml(fromEntity, regexp); LOGINFORMATION( if (hasSameCondition(step->getCondition())) - anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION); + anna::Logger::information(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu, '%s'). Are you sure ?", a_id, a_description.c_str()), ANNA_FILE_LOCATION); ); addStep(step); } -void TestCase::addCommand(const std::string &cmd) throw(anna::RuntimeException) { +void TestCase::addCommand(const std::string &cmd) noexcept(false) { assertInitialized(); TestStepCmd *step = new TestStepCmd(this); @@ -409,13 +498,22 @@ void TestCase::addCommand(const std::string &cmd) throw(anna::RuntimeException) addStep(step); } -TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() { +void TestCase::addIpLimit(unsigned int ipLimit) noexcept(false) { + assertInitialized(); + + TestStepIpLimit *step = new TestStepIpLimit(this); + step->setIpLimit(ipLimit); + + addStep(step); +} + +TestStepWaitDiameter *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) { - TestStepWait *result; + TestStepWaitDiameter *result; for (std::vector::const_iterator it = a_stepsIt /* current */; it != a_steps.end(); it++) { if ((*it)->getType() != TestStep::Type::Wait) continue; if ((*it)->isCompleted()) continue; - result = (TestStepWait*)(*it); + result = (TestStepWaitDiameter*)(*it); if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message))) return result; } @@ -423,7 +521,7 @@ TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock & return NULL; } -const TestStep *TestCase::getStep(int stepNumber) const throw() { +const TestStep *TestCase::getStep(int stepNumber) const { if (stepNumber < 1 || stepNumber > steps()) return NULL; // return a_steps.at(stepNumber-1); // http://stackoverflow.com/questions/3269809/stdvectorat-vs-operator-surprising-results-5-to-10-times-slower-f return a_steps[stepNumber-1];