--- /dev/null
+// ANNA - Anna is Not Nothingness Anymore //
+// //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
+// //
+// See project site at http://redmine.teslayout.com/projects/anna-suite //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+// Standard
+#include <climits>
+
+// Project
+#include <anna/xml/Compiler.hpp>
+#include <anna/xml/Node.hpp>
+#include <anna/core/tracing/Logger.hpp>
+#include <anna/app/functions.hpp>
+#include <anna/timex/Engine.hpp>
+#include <anna/diameter/helpers/base/functions.hpp>
+#include <anna/diameter.comm/ClientSession.hpp>
+#include <anna/diameter.comm/ServerSession.hpp>
+
+// Process
+#include <TestManager.hpp>
+#include <TestCase.hpp>
+#include <TestClock.hpp>
+#include <Launcher.hpp>
+
+
+class TestTimer;
+
+
+TestManager::TestManager() :
+ anna::timex::TimeEventObserver("TestManager") {
+ a_timeController = NULL;
+ a_reportsDirectory = "./";
+ a_dumpReports = false;
+ a_synchronousAmount = 1;
+ a_poolRepeat = false;
+ a_inProgressCount = 0;
+ a_inProgressLimit = UINT_MAX; // no limit
+ a_clock = NULL;
+ //a_testPool.clear();
+ a_currentTestIt = a_testPool.end();
+}
+
+void TestManager::registerSessionId(const std::string &sessionId, const TestCase *testCase) throw(anna::RuntimeException) {
+
+ std::map<std::string /* session id's */, TestCase*>::const_iterator it = a_sessionIdTestCaseMap.find(sessionId);
+ if (it != a_sessionIdTestCaseMap.end()) { // found
+ unsigned int id = it->second->getId();
+ if (id != testCase->getId()) {
+ throw anna::RuntimeException(anna::functions::asString("There is another test case (id = %llu) which registered such sessionId: %s", id, sessionId.c_str()), ANNA_FILE_LOCATION);
+ }
+ }
+ else {
+ a_sessionIdTestCaseMap[sessionId] = const_cast<TestCase*>(testCase);
+ }
+}
+
+TestTimer* TestManager::createTimer(TestCaseStep* testCaseStep, const anna::Millisecond &timeout, const TestTimer::Type::_v type)
+throw(anna::RuntimeException) {
+ TestTimer* result(NULL);
+
+ if(a_timeController == NULL)
+ throw anna::RuntimeException("You must invoke 'setTimerController' with a not NULL timex engine", ANNA_FILE_LOCATION);
+
+ anna::Guard guard(a_timeController, "TestManager::createTimer"); // avoid interblocking
+ result = a_timers.create();
+ result->setType(type);
+ result->setId((anna::timex::TimeEvent::Id) testCaseStep);
+ result->setObserver(this);
+ result->setContext(testCaseStep);
+ result->setTimeout(timeout);
+
+ LOGDEBUG(
+ std::string msg("TestManager::createTimer | ");
+ msg += result->asString();
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ a_timeController->activate(result);
+ return result;
+}
+
+void TestManager::cancelTimer(TestTimer* timer)
+throw() {
+ if(timer == NULL)
+ return;
+
+ LOGDEBUG(
+ std::string msg("TestManager::cancel | ");
+ msg += timer->asString();
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ try {
+ if(a_timeController == NULL)
+ a_timeController = anna::app::functions::component <anna::timex::Engine> (ANNA_FILE_LOCATION);
+
+ a_timeController->cancel(timer);
+ } catch(anna::RuntimeException& ex) {
+ ex.trace();
+ }
+}
+
+//------------------------------------------------------------------------------------------
+// Se invoca automaticamente desde anna::timex::Engine
+//------------------------------------------------------------------------------------------
+void TestManager::release(anna::timex::TimeEvent* timeEvent)
+throw() {
+ TestTimer* timer = static_cast <TestTimer*>(timeEvent);
+ timer->setContext(NULL);
+ a_timers.release(timer);
+}
+
+bool TestManager::configureTTPS(int testTicksPerSecond) throw() {
+
+ if (testTicksPerSecond == 0) {
+ if (a_clock) {
+ a_timeController->cancel(a_clock);
+ LOGDEBUG(anna::Logger::debug("Testing timer clock stopped !", ANNA_FILE_LOCATION));
+ }
+ else {
+ LOGDEBUG(anna::Logger::debug("No testing timer started yet !", ANNA_FILE_LOCATION));
+ }
+ return true;
+ }
+ else if (testTicksPerSecond < 0) {
+ LOGWARNING(anna::Logger::warning("Invalid 'ttps' provided", ANNA_FILE_LOCATION));
+ return false;
+ }
+
+ anna::Millisecond admlTimeInterval = anna::Millisecond(1000 / testTicksPerSecond);
+ a_synchronousAmount = 1;
+
+ if (admlTimeInterval < anna::Millisecond(1)) {
+ LOGWARNING(anna::Logger::warning("Not allowed to configure more than 1000 events per second for the time trigger testing system", ANNA_FILE_LOCATION));
+ return false;
+ }
+
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ const anna::Millisecond &admlMinResolution = my_app.getADMLMinResolution();
+
+ if (admlTimeInterval < admlMinResolution) {
+ int maximumObtained = 1000 / (int)admlMinResolution;
+ a_synchronousAmount = ceil((double)testTicksPerSecond/maximumObtained);
+ // calculate again:
+ admlTimeInterval = anna::Millisecond(a_synchronousAmount * 1000 / testTicksPerSecond);
+ }
+
+ if (a_synchronousAmount > 1) {
+ LOGWARNING(
+ std::string msg = anna::functions::asString("Desired testing time trigger rate (%d events per second) requires more than one sending per event (%d every %lld milliseconds). Consider launch more instances with lower rate (for example %d ADML processes with %d ttps), or configure %d or more sockets to the remote endpoints to avoid burst sendings",
+ testTicksPerSecond,
+ a_synchronousAmount,
+ admlTimeInterval.getValue(),
+ a_synchronousAmount,
+ 1000/admlTimeInterval,
+ a_synchronousAmount);
+
+ anna::Logger::warning(msg, ANNA_FILE_LOCATION);
+ );
+ }
+
+ if (a_clock) {
+ a_clock->setTimeout(admlTimeInterval);
+ }
+ else {
+ a_clock = new TestClock("Testing clock", admlTimeInterval, this); // clock
+ }
+
+ if (!a_clock->isActive()) a_timeController->activate(a_clock);
+
+ return true;
+}
+
+bool TestManager::gotoTestCase(unsigned int id) throw() {
+ test_pool_it it = a_testPool.find(id);
+ if (it != a_testPool.end()) {
+ a_currentTestIt = it;
+ return true;
+ }
+
+ return false;
+}
+
+TestCase *TestManager::findTestCase(unsigned int id) const throw() { // id = -1 provides current test case triggered
+
+ if (!tests()) return NULL;
+ test_pool_it it = ((id != -1) ? a_testPool.find(id) : a_currentTestIt);
+ if (it != a_testPool.end()) return const_cast<TestCase*>(it->second);
+ return NULL;
+}
+
+TestCase *TestManager::getTestCase(unsigned int id) throw() {
+
+ test_pool_nc_it it = a_testPool.find(id);
+ if (it != a_testPool.end()) return it->second;
+
+ TestCase *result = new TestCase(id);
+ a_testPool[id] = result;
+ return result;
+}
+
+bool TestManager::clearPool() throw() {
+ if (!tests()) return false;
+ for (test_pool_it it = a_testPool.begin(); it != a_testPool.end(); it++) delete it->second;
+ a_testPool.clear();
+ a_sessionIdTestCaseMap.clear();
+ a_currentTestIt = a_testPool.end();
+ configureTTPS(0); // stop
+ return true;
+}
+
+bool TestManager::resetPool(bool hard) throw() {
+ bool result = false; // any reset
+
+ if (!tests()) return result;
+ for (test_pool_nc_it it = a_testPool.begin(); it != a_testPool.end(); it++) {
+ it->second->reset(hard);
+ result = true;
+ }
+ //a_sessionIdTestCaseMap.clear();
+ return result;
+}
+
+bool TestManager::tick() throw() {
+ LOGDEBUG(anna::Logger::debug("New test clock tick !", ANNA_FILE_LOCATION));
+
+ if (!tests()) {
+ LOGWARNING(anna::Logger::warning("Testing pool is empty. You need programming. Stopping test clock", ANNA_FILE_LOCATION));
+ return false;
+ }
+
+ int count = a_synchronousAmount;
+ while (count > 0) {
+ if (!nextTestCase()) return false; // stop the clock
+ count--;
+ }
+
+ return true;
+}
+
+bool TestManager::nextTestCase() throw() {
+
+ while (true) {
+
+ // Limit for in-progress test cases:
+ if (a_inProgressCount >= a_inProgressLimit) {
+ LOGDEBUG(anna::Logger::debug(anna::functions::asString("TestManager next case ignored (over in-progress count limit: %llu)", a_inProgressLimit), ANNA_FILE_LOCATION));
+ return true; // wait next tick to release OTA test cases
+ }
+
+ // Next test case:
+ if (a_currentTestIt == a_testPool.end())
+ a_currentTestIt = a_testPool.begin();
+ else
+ a_currentTestIt++;
+
+ // Completed:
+ if (a_currentTestIt == a_testPool.end()) {
+ if (a_poolRepeat) {
+ LOGWARNING(anna::Logger::warning("Testing pool cycle completed. Repeat mode on. Restarting", ANNA_FILE_LOCATION));
+ //a_currentTestIt = a_testPool.begin();
+ return true; // avoids infinite loop: if the cycle takes less time than test cases completion, below reset never will turns state
+ // into Initialized and this while will be infinite. It is preferable to wait one tick when the cycle is completed.
+ }
+ else {
+ LOGWARNING(anna::Logger::warning("Testing pool cycle completed. Repeat mode off. Suspending", ANNA_FILE_LOCATION));
+ return false;
+ }
+ }
+
+ // Hard reset, because normally a cycle takes more time that a single test case lifetime. We can consider that never
+ // going to break a in-progress test case due to cycle repeat
+ a_currentTestIt->second->reset(false);
+
+ // Process test case:
+ LOGDEBUG(anna::Logger::debug(anna::functions::asString("Processing test case id = %llu, currently '%s' state", a_currentTestIt->first, TestCase::asText(a_currentTestIt->second->getState())), ANNA_FILE_LOCATION));
+ if (a_currentTestIt->second->getState() != TestCase::State::InProgress) {
+ a_currentTestIt->second->process();
+ return true;
+ }
+ }
+}
+
+TestCase *TestManager::getTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw(anna::RuntimeException) {
+ sessionId = anna::diameter::helpers::base::functions::getSessionId(message);
+ std::map<std::string /* session id's */, TestCase*>::const_iterator sessionIdIt = a_sessionIdTestCaseMap.find(sessionId);
+ if (sessionIdIt != a_sessionIdTestCaseMap.end())
+ return sessionIdIt->second;
+
+ LOGWARNING(anna::Logger::warning(anna::functions::asString("Cannot identify the Test Case for received Session-Id: %s", sessionId.c_str()), ANNA_FILE_LOCATION));
+ return NULL;
+}
+
+void TestManager::receiveMessage(const anna::DataBlock &message, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException) {
+
+ // Testing disabled:
+ if (!tests()) return;
+
+ // Identify the test case:
+ std::string sessionId;
+ TestCase *tc = getTestCaseFromSessionId(message, sessionId);
+ if (!tc) return;
+
+ // Work with Test case:
+ TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, true /* comes from entity */);
+ if (!tsw) { // store as 'uncovered'
+ std::string hint = "Uncovered condition for received message from entity over Session-Id '"; hint += sessionId; hint += "':";
+
+ try {
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
+ codecMsg.decode(message);
+ hint += "\n"; hint += codecMsg.asXMLString();
+ }
+ catch (anna::RuntimeException &ex) {
+ ex.trace();
+ hint += "\n"; hint += ex.asString();
+ }
+ hint += "\n"; hint += clientSession->asString();
+
+ tc->addDebugSummaryHint(hint);
+ }
+ else {
+ tsw->setClientSession(const_cast<anna::diameter::comm::ClientSession*>(clientSession));
+ }
+}
+
+void TestManager::receiveMessage(const anna::DataBlock &message, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException) {
+
+ // Testing disabled:
+ if (!tests()) return;
+
+ // Identify the test case:
+ std::string sessionId;
+ TestCase *tc = getTestCaseFromSessionId(message, sessionId);
+ if (!tc) return;
+
+ // Work with Test case:
+ TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, false /* comes from client */);
+ if (!tsw) { // store as 'uncovered'
+ std::string hint = "Uncovered condition for received message from client over Session-Id '"; hint += sessionId; hint += "':";
+
+ try {
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
+ codecMsg.decode(message);
+ hint += "\n"; hint += codecMsg.asXMLString();
+ }
+ catch (anna::RuntimeException &ex) {
+ ex.trace();
+ hint += "\n"; hint += ex.asString();
+ }
+ hint += "\n"; hint += serverSession->asString();
+
+ tc->addDebugSummaryHint(hint);
+ }
+ else {
+ tsw->setServerSession(const_cast<anna::diameter::comm::ServerSession*>(serverSession));
+ }
+}
+
+anna::xml::Node* TestManager::asXML(anna::xml::Node* parent) const
+throw() {
+ anna::xml::Node* result = parent->createChild("TestManager");
+
+ int poolSize = a_testPool.size();
+ result->createAttribute("NumberOfTestCases", poolSize);
+ result->createAttribute("PoolRepeat", (a_poolRepeat ? "yes":"no"));
+ result->createAttribute("InProgressCount", a_inProgressCount);
+ if (a_inProgressLimit == UINT_MAX)
+ result->createAttribute("InProgressLimit", "<no limit>");
+ else
+ result->createAttribute("InProgressLimit", a_inProgressLimit);
+ result->createAttribute("DumpReports", (a_dumpReports ? "yes":"no"));
+ result->createAttribute("ReportsDirectory", a_reportsDirectory);
+ if (a_clock) {
+ result->createAttribute("AsynchronousSendings", a_synchronousAmount);
+ int ticksPerSecond = (a_synchronousAmount * 1000) / a_clock->getTimeout();
+ result->createAttribute("TicksPerSecond", ticksPerSecond);
+ }
+ if (a_currentTestIt != a_testPool.end()) {
+ result->createAttribute("CurrentTestCaseId", (*a_currentTestIt).first);
+ }
+ if (a_dumpReports && poolSize != 0) {
+ anna::xml::Node* testCases = result->createChild("TestCases");
+ for (test_pool_it it = a_testPool.begin(); it != a_testPool.end(); it++) {
+ (*it).second->asXML(testCases);
+ }
+ }
+
+ return result;
+}
+
+std::string TestManager::asXMLString() const throw() {
+ anna::xml::Node root("root");
+ return anna::xml::Compiler().apply(asXML(&root));
+}
+