1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
13 #include <anna/xml/Compiler.hpp>
14 #include <anna/xml/Node.hpp>
15 #include <anna/core/tracing/Logger.hpp>
16 #include <anna/app/functions.hpp>
17 #include <anna/timex/Engine.hpp>
18 #include <anna/diameter/helpers/base/functions.hpp>
19 #include <anna/diameter.comm/ClientSession.hpp>
20 #include <anna/diameter.comm/ServerSession.hpp>
23 #include <TestManager.hpp>
24 #include <TestCase.hpp>
25 #include <TestClock.hpp>
26 #include <Launcher.hpp>
32 TestManager::TestManager() :
33 anna::timex::TimeEventObserver("TestManager") {
34 a_timeController = NULL;
35 a_reportsDirectory = "./";
36 a_dumpReports = false;
37 a_dumpHexMessages = false;
38 a_synchronousAmount = 1;
40 a_inProgressCount = 0;
41 a_inProgressLimit = UINT_MAX; // no limit
44 a_currentTestIt = a_testPool.end();
47 void TestManager::registerSessionId(const std::string &sessionId, const TestCase *testCase) throw(anna::RuntimeException) {
49 std::map<std::string /* session id's */, TestCase*>::const_iterator it = a_sessionIdTestCaseMap.find(sessionId);
50 if (it != a_sessionIdTestCaseMap.end()) { // found
51 unsigned int id = it->second->getId();
52 if (id != testCase->getId()) {
53 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);
57 a_sessionIdTestCaseMap[sessionId] = const_cast<TestCase*>(testCase);
61 TestTimer* TestManager::createTimer(TestCaseStep* testCaseStep, const anna::Millisecond &timeout, const TestTimer::Type::_v type)
62 throw(anna::RuntimeException) {
63 TestTimer* result(NULL);
65 if(a_timeController == NULL)
66 throw anna::RuntimeException("You must invoke 'setTimerController' with a not NULL timex engine", ANNA_FILE_LOCATION);
68 anna::Guard guard(a_timeController, "TestManager::createTimer"); // avoid interblocking
69 result = a_timers.create();
70 result->setType(type);
71 result->setId((anna::timex::TimeEvent::Id) testCaseStep);
72 result->setObserver(this);
73 result->setContext(testCaseStep);
74 result->setTimeout(timeout);
77 std::string msg("TestManager::createTimer | ");
78 msg += result->asString();
79 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
82 a_timeController->activate(result);
86 void TestManager::cancelTimer(TestTimer* timer)
92 std::string msg("TestManager::cancel | ");
93 msg += timer->asString();
94 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
98 if(a_timeController == NULL)
99 a_timeController = anna::app::functions::component <anna::timex::Engine> (ANNA_FILE_LOCATION);
101 a_timeController->cancel(timer);
102 } catch(anna::RuntimeException& ex) {
107 //------------------------------------------------------------------------------------------
108 // Se invoca automaticamente desde anna::timex::Engine
109 //------------------------------------------------------------------------------------------
110 void TestManager::release(anna::timex::TimeEvent* timeEvent)
112 TestTimer* timer = static_cast <TestTimer*>(timeEvent);
113 timer->setContext(NULL);
114 a_timers.release(timer);
117 bool TestManager::configureTTPS(int testTicksPerSecond) throw() {
119 if (testTicksPerSecond == 0) {
121 a_timeController->cancel(a_clock);
122 LOGDEBUG(anna::Logger::debug("Testing timer clock stopped !", ANNA_FILE_LOCATION));
125 LOGDEBUG(anna::Logger::debug("No testing timer started yet !", ANNA_FILE_LOCATION));
129 else if (testTicksPerSecond < 0) {
130 LOGWARNING(anna::Logger::warning("Invalid 'ttps' provided", ANNA_FILE_LOCATION));
134 anna::Millisecond admlTimeInterval = anna::Millisecond(1000 / testTicksPerSecond);
135 a_synchronousAmount = 1;
137 if (admlTimeInterval < anna::Millisecond(1)) {
138 LOGWARNING(anna::Logger::warning("Not allowed to configure more than 1000 events per second for the time trigger testing system", ANNA_FILE_LOCATION));
142 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
143 const anna::Millisecond &admlMinResolution = my_app.getADMLMinResolution();
145 if (admlTimeInterval < admlMinResolution) {
146 int maximumObtained = 1000 / (int)admlMinResolution;
147 a_synchronousAmount = ceil((double)testTicksPerSecond/maximumObtained);
149 admlTimeInterval = anna::Millisecond(a_synchronousAmount * 1000 / testTicksPerSecond);
152 if (a_synchronousAmount > 1) {
154 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",
157 admlTimeInterval.getValue(),
159 1000/admlTimeInterval,
160 a_synchronousAmount);
162 anna::Logger::warning(msg, ANNA_FILE_LOCATION);
167 a_clock->setTimeout(admlTimeInterval);
170 a_clock = new TestClock("Testing clock", admlTimeInterval, this); // clock
173 if (!a_clock->isActive()) a_timeController->activate(a_clock);
178 bool TestManager::gotoTestCase(unsigned int id) throw() {
179 test_pool_it it = a_testPool.find(id);
180 if (it != a_testPool.end()) {
181 a_currentTestIt = it;
188 TestCase *TestManager::findTestCase(unsigned int id) const throw() { // id = -1 provides current test case triggered
190 if (!tests()) return NULL;
191 test_pool_it it = ((id != -1) ? a_testPool.find(id) : a_currentTestIt);
192 if (it != a_testPool.end()) return const_cast<TestCase*>(it->second);
196 TestCase *TestManager::getTestCase(unsigned int id) throw() {
198 test_pool_nc_it it = a_testPool.find(id);
199 if (it != a_testPool.end()) return it->second;
201 TestCase *result = new TestCase(id);
202 a_testPool[id] = result;
206 bool TestManager::clearPool() throw() {
207 if (!tests()) return false;
208 for (test_pool_it it = a_testPool.begin(); it != a_testPool.end(); it++) delete it->second;
210 a_sessionIdTestCaseMap.clear();
211 a_currentTestIt = a_testPool.end();
212 configureTTPS(0); // stop
216 bool TestManager::resetPool(bool hard) throw() {
217 bool result = false; // any reset
219 if (!tests()) return result;
220 for (test_pool_nc_it it = a_testPool.begin(); it != a_testPool.end(); it++) {
221 it->second->reset(hard);
224 //a_sessionIdTestCaseMap.clear();
228 bool TestManager::tick() throw() {
229 LOGDEBUG(anna::Logger::debug("New test clock tick !", ANNA_FILE_LOCATION));
232 LOGWARNING(anna::Logger::warning("Testing pool is empty. You need programming. Stopping test clock", ANNA_FILE_LOCATION));
236 // Synchronous sendings per tick:
237 int count = a_synchronousAmount;
239 if (!nextTestCase()) return false; // stop the clock
246 bool TestManager::nextTestCase() throw() {
250 // Limit for in-progress test cases:
251 if (a_inProgressCount >= a_inProgressLimit) {
252 LOGDEBUG(anna::Logger::debug(anna::functions::asString("TestManager next case ignored (over in-progress count limit: %llu)", a_inProgressLimit), ANNA_FILE_LOCATION));
253 return true; // wait next tick to release OTA test cases
257 if (a_currentTestIt == a_testPool.end())
258 a_currentTestIt = a_testPool.begin();
263 if (a_currentTestIt == a_testPool.end()) {
265 LOGWARNING(anna::Logger::warning("Testing pool cycle completed. Repeat mode on. Restarting", ANNA_FILE_LOCATION));
266 //a_currentTestIt = a_testPool.begin();
267 return true; // avoids infinite loop: if the cycle takes less time than test cases completion, below reset never will turns state
268 // into Initialized and this while will be infinite. It is preferable to wait one tick when the cycle is completed.
271 LOGWARNING(anna::Logger::warning("Testing pool cycle completed. Repeat mode off. Suspending", ANNA_FILE_LOCATION));
276 // Soft reset to initialize already finished (in previous cycle) test cases:
277 a_currentTestIt->second->reset(false);
279 // Process test case:
280 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));
281 if (a_currentTestIt->second->getState() != TestCase::State::InProgress) {
282 a_currentTestIt->second->process();
283 return true; // is not probably to reach still In-Progress test cases from previous cycles due to the whole
284 // time for complete the test cases pool regarding the single test case lifetime. You shouldn't
285 // forget to programm a test case timeout with a reasonable value
290 TestCase *TestManager::getTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw() {
292 sessionId = anna::diameter::helpers::base::functions::getSessionId(message);
294 catch (anna::RuntimeException &ex) {
296 LOGWARNING(anna::Logger::warning("Cannot get the Session-Id from received DataBlock in order to identify the Test Case", ANNA_FILE_LOCATION));
299 std::map<std::string /* session id's */, TestCase*>::const_iterator sessionIdIt = a_sessionIdTestCaseMap.find(sessionId);
300 if (sessionIdIt != a_sessionIdTestCaseMap.end())
301 return sessionIdIt->second;
303 LOGWARNING(anna::Logger::warning(anna::functions::asString("Cannot identify the Test Case for received Session-Id: %s", sessionId.c_str()), ANNA_FILE_LOCATION));
307 void TestManager::receiveMessage(const anna::DataBlock &message, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException) {
310 if (!tests()) return;
312 // Identify the test case:
313 std::string sessionId;
314 TestCase *tc = getTestCaseFromSessionId(message, sessionId);
317 // Work with Test case:
318 TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, true /* comes from entity */);
319 if (!tsw) { // store as 'uncovered'
320 std::string hint = "Uncovered condition for received message from entity over Session-Id '"; hint += sessionId; hint += "':";
323 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
324 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
325 codecMsg.decode(message);
326 hint += "\n"; hint += codecMsg.asXMLString();
328 catch (anna::RuntimeException &ex) {
330 hint += "\n"; hint += ex.asString();
332 hint += "\n"; hint += clientSession->asString();
334 tc->addDebugSummaryHint(hint);
337 tsw->setClientSession(const_cast<anna::diameter::comm::ClientSession*>(clientSession));
341 void TestManager::receiveMessage(const anna::DataBlock &message, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException) {
344 if (!tests()) return;
346 // Identify the test case:
347 std::string sessionId;
348 TestCase *tc = getTestCaseFromSessionId(message, sessionId);
351 // Work with Test case:
352 TestStepWait *tsw = tc->searchNextWaitConditionFulfilled(message, false /* comes from client */);
353 if (!tsw) { // store as 'uncovered'
354 std::string hint = "Uncovered condition for received message from client over Session-Id '"; hint += sessionId; hint += "':";
357 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
358 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
359 codecMsg.decode(message);
360 hint += "\n"; hint += codecMsg.asXMLString();
362 catch (anna::RuntimeException &ex) {
364 hint += "\n"; hint += ex.asString();
366 hint += "\n"; hint += serverSession->asString();
368 tc->addDebugSummaryHint(hint);
371 tsw->setServerSession(const_cast<anna::diameter::comm::ServerSession*>(serverSession));
375 anna::xml::Node* TestManager::asXML(anna::xml::Node* parent) const
377 anna::xml::Node* result = parent->createChild("TestManager");
379 int poolSize = a_testPool.size();
380 result->createAttribute("NumberOfTestCases", poolSize);
381 result->createAttribute("PoolRepeat", (a_poolRepeat ? "yes":"no"));
382 result->createAttribute("InProgressCount", a_inProgressCount);
383 if (a_inProgressLimit == UINT_MAX)
384 result->createAttribute("InProgressLimit", "<no limit>");
386 result->createAttribute("InProgressLimit", a_inProgressLimit);
387 result->createAttribute("DumpReports", (a_dumpReports ? "yes":"no"));
388 result->createAttribute("DumpHexMessages", (a_dumpHexMessages ? "yes":"no"));
389 result->createAttribute("ReportsDirectory", a_reportsDirectory);
391 result->createAttribute("AsynchronousSendings", a_synchronousAmount);
392 int ticksPerSecond = (a_synchronousAmount * 1000) / a_clock->getTimeout();
393 result->createAttribute("TicksPerSecond", ticksPerSecond);
395 if (a_currentTestIt != a_testPool.end()) {
396 result->createAttribute("CurrentTestCaseId", (*a_currentTestIt).first);
398 if (a_dumpReports && poolSize != 0) {
399 anna::xml::Node* testCases = result->createChild("TestCases");
400 for (test_pool_it it = a_testPool.begin(); it != a_testPool.end(); it++) {
401 (*it).second->asXML(testCases);
408 std::string TestManager::asXMLString() const throw() {
409 anna::xml::Node root("root");
410 return anna::xml::Compiler().apply(asXML(&root));