Dynamic lib selection and deployment
[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 OriginHost;
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     StatSummary() { clear(); }
66     void newTCState(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw();
67     void clear() throw();
68     unsigned int getInProgressCount() const throw() { return a_inprogressTcs; }
69     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
70   };
71
72
73
74   typedef anna::Recycler <TestTimer> timer_container;
75
76   anna::timex::Engine* a_timeController;
77
78   // reports
79   std::string a_reportsDirectory;
80   bool a_dumpInitializedReports, a_dumpInProgressReports, a_dumpFailedReports, a_dumpSuccessReports;
81   bool a_dumpHexMessages;
82
83   // Pool of test cases
84   test_pool_t a_testPool;
85   test_pool_it a_currentTestIt;
86   int a_poolRepeats; // repeat pool N times
87   int a_poolCycle; // current cycle, from 1 to N
88   unsigned int a_inProgressLimit; // limit load to have this value
89
90   // Test clock
91   int a_synchronousAmount;
92   TestClock *a_clock;
93   bool tick() throw();
94   bool execTestCases(int sync_amount) throw();
95   bool nextTestCase() throw();
96
97   // Test timers
98   timer_container a_timers;
99
100   // Test case identifiers:
101   // Session-Id's
102   std::map<std::string /* session id's */, TestCase*> a_sessionIdTestCaseMap; // stores used Session-Id values within a test case.
103                                                                               // No other can use them, but a test case could use more than one.
104   // Subscriber's
105   std::map<std::string /* subscriber id's */, TestCase*> a_subscriberIdTestCaseMap; // stores used Subscriber-Id values within a test case.
106
107
108
109   StatSummary a_statSummary; // general statistics
110
111
112   TestManager();
113   TestManager(const TestManager&);
114
115   TestTimer* createTimer(TestCaseStep*, const anna::Millisecond &, const TestTimer::Type::_v type) throw(anna::RuntimeException);
116   void cancelTimer(TestTimer*) throw();
117   void release(anna::timex::TimeEvent*) throw();
118
119
120   public:
121
122     void registerSessionId(const std::string &sessionId, const TestCase *testCase) throw(anna::RuntimeException);
123     void registerSubscriberId(const std::string &subscriberId, const TestCase *testCase) throw(anna::RuntimeException);
124
125     int tests() const throw() { return a_testPool.size(); }
126     void setTimerController(anna::timex::Engine *engine) throw() {  a_timeController = engine; }
127
128     void setReportsDirectory(const std::string &rd) throw() { a_reportsDirectory = rd; }
129     const std::string &getReportsDirectory() const throw() { return a_reportsDirectory; }
130
131     void setDumpHex(bool dh) throw() { a_dumpHexMessages = dh; }
132     bool getDumpHex() const throw() { return a_dumpHexMessages; }
133
134
135     void setDumpInitializedReports(bool enable) throw() { a_dumpInitializedReports = enable; }
136     void setDumpInProgressReports(bool enable) throw() { a_dumpInProgressReports = enable; }
137     void setDumpFailedReports(bool enable) throw() { a_dumpFailedReports = enable; }
138     void setDumpSuccessReports(bool enable) throw() { a_dumpSuccessReports = enable; }
139     void setDumpAllReports(bool enable) throw() {
140       setDumpInitializedReports(enable);
141       setDumpInProgressReports(enable);
142       setDumpFailedReports(enable);
143       setDumpSuccessReports(enable);
144     }
145
146     bool getDumpInitializedReports() const throw() { return a_dumpInitializedReports; }
147     bool getDumpInProgressReports() const throw() { return a_dumpInProgressReports; }
148     bool getDumpFailedReports() const throw() { return a_dumpFailedReports; }
149     bool getDumpSuccessReports() const throw() { return a_dumpSuccessReports; }
150
151     // Helper to calculate time interval and synchronous amount of execution tests to guarantee the input rate (tests per second)
152     //  through the time manager which has a minimum resolution of ADML minimum resolution. The first call to this method will
153     //  start the time trigger system and check for new test cases to be launched.
154     bool configureTTPS(int testTicksPerSecond) throw();
155
156     bool clearPool() throw();
157     bool resetPool(bool hard /* hard reset includes in-progress test cases */) throw();
158     void setPoolRepeats(int repeats) throw() { a_poolRepeats = repeats; }
159     int getPoolRepeats() const throw() { return a_poolRepeats; }
160     int getPoolCycle() const throw() { return a_poolCycle; }
161
162     unsigned int getInProgressCount() const throw() { return a_statSummary.getInProgressCount(); }
163     unsigned int getInProgressLimit() const throw() { return a_inProgressLimit; }
164     void setInProgressLimit(unsigned int limit) throw() { a_inProgressLimit = limit; } // 0 = UINT_MAX (no limit)
165
166     bool gotoTestCase(unsigned int id) throw();
167     TestCase *findTestCase(unsigned int id) const throw(); // id = -1 provides current test case triggered
168     TestCase *getTestCase(unsigned int id) throw(); // creates/reuses a test case
169
170     // Main logic
171     TestCase *getTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw();
172     TestCase *getTestCaseFromSubscriberId(const anna::DataBlock &message, std::string &subscriberId) throw();
173     void receiveMessage(const anna::DataBlock &message, OriginHost *host, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException);
174     void receiveMessage(const anna::DataBlock &message, OriginHost *host, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException);
175
176     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
177     std::string asXMLString() const throw();
178
179     // stats
180     void tcsStateStats(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw() {
181       a_statSummary.newTCState(beginState, endState);
182     }
183
184
185   friend class anna::Singleton <TestManager>;
186   friend class TestStepTimeout; // createTimer
187   friend class TestStepDelay; // createTimer
188   friend class TestClock; // tick
189   friend class Launcher; // tick
190 };
191
192 #endif