New st-client deployment. Allow to load same stack id (accumulative services), ignori...
[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 #include <TestCase.hpp>
23
24
25 namespace anna {
26   class Millisecond;
27
28   namespace timex {
29     class Engine;
30   }
31   namespace diameter {
32     namespace comm {
33       class ClientSession;
34       class ServerSession;
35     }
36   }
37 }
38
39
40 class TestClock;
41 class TestCase;
42 class TestCaseStep;
43 class RealmNode;
44
45
46 typedef std::map<unsigned int /* test case id */, TestCase*> test_pool_t;
47 typedef std::map<unsigned int /* test case id */, TestCase*>::const_iterator test_pool_it;
48 typedef std::map<unsigned int /* test case id */, TestCase*>::iterator test_pool_nc_it;
49
50
51 /**
52    Timer Manager for testing system
53 */
54 class TestManager : public anna::timex::TimeEventObserver, public anna::Singleton <TestManager> {
55
56   // Statistics summary:
57   class StatSummary {
58
59     unsigned int a_initializedTcs;
60     unsigned int a_inprogressTcs;
61     unsigned int a_failedTcs;
62     unsigned int a_sucessTcs;
63
64   public:
65     void newTCState(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw();
66     void clear() throw();
67     unsigned int getInProgressCount() const throw() { return a_inprogressTcs; }
68     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
69   };
70
71
72
73   typedef anna::Recycler <TestTimer> timer_container;
74
75   anna::timex::Engine* a_timeController;
76
77   // reports
78   std::string a_reportsDirectory;
79   bool a_dumpReports;
80   bool a_dumpHexMessages;
81
82   // Pool of test cases
83   test_pool_t a_testPool;
84   test_pool_it a_currentTestIt;
85   int a_poolRepeats; // repeat pool N times
86   int a_poolCycle; // current cycle, from 1 to N
87   unsigned int a_inProgressLimit; // limit load to have this value
88
89   // Test clock
90   int a_synchronousAmount;
91   TestClock *a_clock;
92   bool tick() throw();
93   bool execTestCases(int sync_amount) throw();
94   bool nextTestCase() throw();
95
96   // Test timers
97   timer_container a_timers;
98
99   // Session-Id's
100   std::map<std::string /* session id's */, TestCase*> a_sessionIdTestCaseMap; // stores used Session-Id values within a test case.
101                                                                               // No other can use them, but a test case could use more than one.
102
103   StatSummary a_statSummary; // general statistics
104
105
106   TestManager();
107   TestManager(const TestManager&);
108
109   TestTimer* createTimer(TestCaseStep*, const anna::Millisecond &, const TestTimer::Type::_v type) throw(anna::RuntimeException);
110   void cancelTimer(TestTimer*) throw();
111   void release(anna::timex::TimeEvent*) throw();
112
113
114   public:
115
116     void registerSessionId(const std::string &sessionId, const TestCase *testCase) throw(anna::RuntimeException);
117
118     int tests() const throw() { return a_testPool.size(); }
119     void setTimerController(anna::timex::Engine *engine) throw() {  a_timeController = engine; }
120
121     void setReportsDirectory(const std::string &rd) throw() { a_reportsDirectory = rd; }
122     const std::string &getReportsDirectory() const throw() { return a_reportsDirectory; }
123
124     void setDumpHex(bool dh) throw() { a_dumpHexMessages = dh; }
125     bool getDumpHex() const throw() { return a_dumpHexMessages; }
126     void setDumpReports(bool dr) throw() { a_dumpReports = dr; }
127     bool getDumpReports() const throw() { return a_dumpReports; }
128
129     // Helper to calculate time interval and synchronous amount of execution tests to guarantee the input rate (tests per second)
130     //  through the time manager which has a minimum resolution of ADML minimum resolution. The first call to this method will
131     //  start the time trigger system and check for new test cases to be launched.
132     bool configureTTPS(int testTicksPerSecond) throw();
133
134     bool clearPool() throw();
135     bool resetPool(bool hard /* hard reset includes in-progress test cases */) throw();
136     void setPoolRepeats(int repeats) throw() { a_poolRepeats = repeats; }
137     int getPoolRepeats() const throw() { return a_poolRepeats; }
138     int getPoolCycle() const throw() { return a_poolCycle; }
139
140     unsigned int getInProgressCount() const throw() { return a_statSummary.getInProgressCount(); }
141     unsigned int getInProgressLimit() const throw() { return a_inProgressLimit; }
142     void setInProgressLimit(unsigned int limit) throw() { a_inProgressLimit = limit; } // 0 = UINT_MAX (no limit)
143
144     bool gotoTestCase(unsigned int id) throw();
145     TestCase *findTestCase(unsigned int id) const throw(); // id = -1 provides current test case triggered
146     TestCase *getTestCase(unsigned int id) throw(); // creates/reuses a test case
147
148     // Main logic
149     TestCase *getTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw();
150     void receiveMessage(const anna::DataBlock &message, RealmNode *realm, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException);
151     void receiveMessage(const anna::DataBlock &message, RealmNode *realm, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException);
152
153     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
154     std::string asXMLString() const throw();
155
156     // stats
157     void tcsStateStats(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw() {
158       a_statSummary.newTCState(beginState, endState);
159     }
160
161
162   friend class anna::Singleton <TestManager>;
163   friend class TestStepTimeout; // createTimer
164   friend class TestStepDelay; // createTimer
165   friend class TestClock; // tick
166   friend class Launcher; // tick
167 };
168
169 #endif