Hard refactoring. CodecEngine is associated to a unique stack.
[anna.git] / example / diameter / launcher / testing / TestManager.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef example_diameter_launcher_TestManager_hpp
10 #define example_diameter_launcher_TestManager_hpp
11
12 // Project
13 #include <anna/config/defines.hpp>
14 #include <anna/core/util/Recycler.hpp>
15 #include <anna/core/Singleton.hpp>
16 #include <anna/timex/TimeEventObserver.hpp>
17 #include <anna/core/RuntimeException.hpp>
18 #include <anna/core/DataBlock.hpp>
19
20 // Process
21 #include <TestTimer.hpp>
22
23
24 namespace anna {
25   class Millisecond;
26
27   namespace timex {
28     class Engine;
29   }
30   namespace diameter {
31     namespace comm {
32       class ClientSession;
33       class ServerSession;
34     }
35   }
36 }
37
38
39 class TestClock;
40 class TestCase;
41 class TestCaseStep;
42 class RealmNode;
43
44
45 typedef std::map<unsigned int /* test case id */, TestCase*> test_pool_t;
46 typedef std::map<unsigned int /* test case id */, TestCase*>::const_iterator test_pool_it;
47 typedef std::map<unsigned int /* test case id */, TestCase*>::iterator test_pool_nc_it;
48
49
50 /**
51    Timer Manager for testing system
52 */
53 class TestManager : public anna::timex::TimeEventObserver, public anna::Singleton <TestManager> {
54   typedef anna::Recycler <TestTimer> timer_container;
55
56   anna::timex::Engine* a_timeController;
57
58   // reports
59   std::string a_reportsDirectory;
60   bool a_dumpReports;
61   bool a_dumpHexMessages;
62
63   // Pool of test cases
64   test_pool_t a_testPool;
65   test_pool_it a_currentTestIt;
66   int a_poolRepeats; // repeat pool N times
67   int a_poolCycle; // current cycle, from 1 to N
68   unsigned int a_inProgressCount;
69   unsigned int a_inProgressLimit; // limit load to have this value
70
71   // Test clock
72   int a_synchronousAmount;
73   TestClock *a_clock;
74   bool tick() throw();
75   bool nextTestCase() throw();
76
77   // Test timers
78   timer_container a_timers;
79
80   // Session-Id's
81   std::map<std::string /* session id's */, TestCase*> a_sessionIdTestCaseMap; // stores used Session-Id values within a test case.
82                                                                               // No other can use them, but a test case could use more than one.
83
84
85   TestManager();
86   TestManager(const TestManager&);
87
88   TestTimer* createTimer(TestCaseStep*, const anna::Millisecond &, const TestTimer::Type::_v type) throw(anna::RuntimeException);
89   void cancelTimer(TestTimer*) throw();
90   void release(anna::timex::TimeEvent*) throw();
91
92   public:
93
94     void registerSessionId(const std::string &sessionId, const TestCase *testCase) throw(anna::RuntimeException);
95
96     int tests() const throw() { return a_testPool.size(); }
97     void setTimerController(anna::timex::Engine *engine) throw() {  a_timeController = engine; }
98
99     void setReportsDirectory(const std::string &rd) throw() { a_reportsDirectory = rd; }
100     const std::string &getReportsDirectory() const throw() { return a_reportsDirectory; }
101
102     void setDumpHex(bool dh) throw() { a_dumpHexMessages = dh; }
103     bool getDumpHex() const throw() { return a_dumpHexMessages; }
104     void setDumpReports(bool dr) throw() { a_dumpReports = dr; }
105     bool getDumpReports() const throw() { return a_dumpReports; }
106
107     // Helper to calculate time interval and synchronous amount of execution tests to guarantee the input rate (tests per second)
108     //  through the time manager which has a minimum resolution of ADML minimum resolution. The first call to this method will
109     //  start the time trigger system and check for new test cases to be launched.
110     bool configureTTPS(int testTicksPerSecond) throw();
111
112     bool clearPool() throw();
113     bool resetPool(bool hard /* hard reset includes in-progress test cases */) throw();
114     void setPoolRepeats(int repeats) throw() { a_poolRepeats = repeats; }
115     int getPoolRepeats() const throw() { return a_poolRepeats; }
116     int getPoolCycle() const throw() { return a_poolCycle; }
117
118     unsigned int getInProgressCount() const throw() { return a_inProgressCount; }
119     void setInProgressCountDelta(unsigned int delta) throw() { a_inProgressCount += delta; }
120     unsigned int getInProgressLimit() const throw() { return a_inProgressLimit; }
121     void setInProgressLimit(unsigned int limit) throw() { a_inProgressLimit = limit; } // 0 = UINT_MAX (no limit)
122
123     bool gotoTestCase(unsigned int id) throw();
124     TestCase *findTestCase(unsigned int id) const throw(); // id = -1 provides current test case triggered
125     TestCase *getTestCase(unsigned int id) throw(); // creates/reuses a test case
126
127     // Main logic
128     TestCase *getTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw();
129     void receiveMessage(const anna::DataBlock &message, RealmNode *realm, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException);
130     void receiveMessage(const anna::DataBlock &message, RealmNode *realm, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException);
131
132     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
133     std::string asXMLString() const throw();
134
135
136   friend class anna::Singleton <TestManager>;
137   friend class TestStepTimeout; // createTimer
138   friend class TestStepDelay; // createTimer
139   friend class TestClock; // tick
140 };
141
142 #endif