82fdf990182afa0df70262387eed5a50d497ff18
[anna.git] / include / anna / 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 anna_testing_TestManager_hpp
10 #define anna_testing_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 #include <anna/testing/TestTimer.hpp>
20 #include <anna/testing/TestCase.hpp>
21
22
23 namespace anna {
24   class Millisecond;
25
26   namespace timex {
27     class Engine;
28   }
29   namespace diameter {
30     namespace comm {
31       class ClientSession;
32       class ServerSession;
33     }
34   }
35
36 namespace testing {
37
38
39 class TestClock;
40 class TestCase;
41 class TestCaseStep;
42
43
44 typedef std::map<unsigned int /* test case id */, TestCase*> test_pool_t;
45 typedef std::map<unsigned int /* test case id */, TestCase*>::const_iterator test_pool_it;
46 typedef std::map<unsigned int /* test case id */, TestCase*>::iterator test_pool_nc_it;
47
48
49 /**
50    Timer Manager for testing system
51 */
52 class TestManager : public anna::timex::TimeEventObserver, public anna::Singleton <TestManager> {
53
54   // Statistics summary:
55   class StatSummary {
56
57     unsigned int a_initializedTcs;
58     unsigned int a_inprogressTcs;
59     unsigned int a_failedTcs;
60     unsigned int a_sucessTcs;
61
62   public:
63     StatSummary() { clear(); }
64     void newTCState(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw();
65     void clear() throw();
66     unsigned int getInProgressCount() const throw() { return a_inprogressTcs; }
67     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
68   };
69
70
71
72   typedef anna::Recycler <TestTimer> timer_container;
73
74   anna::timex::Engine* a_timeController;
75
76   // reports
77   std::string a_reportsDirectory;
78   bool a_dumpInitializedReports, a_dumpInProgressReports, a_dumpFailedReports, a_dumpSuccessReports;
79   bool a_dumpHexMessages;
80
81   // Pool of test cases
82   test_pool_t a_testPool;
83   test_pool_it a_currentTestIt;
84   int a_poolRepeats; // repeat pool N times
85   int a_poolCycle; // current cycle, from 1 to N
86   unsigned int a_inProgressLimit; // limit load to have this value
87   bool a_autoResetHard; // automatic reset on next cycle (soft by default, false)
88
89   // Test clock
90   int a_synchronousAmount;
91   TestClock *a_clock;
92   bool tick() throw();
93   bool nextTestCase() throw();
94
95   // Test timers
96   timer_container a_timers;
97
98   // Test case identifiers: key1 (strong); key2 (alternative)
99   std::map<std::string /* key */, TestCase*> a_key1TestCaseMap;
100   std::map<std::string /* key */, TestCase*> a_key2TestCaseMap;
101
102   // Diameter shall use: key1 -> sessionId, key2 -> subscriberId
103   //
104
105   StatSummary a_statSummary; // general statistics
106
107
108   TestManager();
109   TestManager(const TestManager&);
110   virtual ~TestManager() {;}
111
112   TestTimer* createTimer(TestCaseStep*, const anna::Millisecond &, const TestTimer::Type::_v type) throw(anna::RuntimeException);
113   void cancelTimer(TestTimer*) throw();
114   void release(anna::timex::TimeEvent*) throw();
115
116
117   public:
118
119     void registerKey1(const std::string &key, const TestCase *testCase) throw(anna::RuntimeException);
120     void registerKey2(const std::string &key, const TestCase *testCase) throw(anna::RuntimeException);
121
122     int tests() const throw() { return a_testPool.size(); }
123     void setTimerController(anna::timex::Engine *engine) throw() {  a_timeController = engine; }
124
125     void setReportsDirectory(const std::string &rd) throw() { a_reportsDirectory = rd; }
126     const std::string &getReportsDirectory() const throw() { return a_reportsDirectory; }
127
128     void setDumpHex(bool dh) throw() { a_dumpHexMessages = dh; }
129     bool getDumpHex() const throw() { return a_dumpHexMessages; }
130
131
132     void setDumpInitializedReports(bool enable) throw() { a_dumpInitializedReports = enable; }
133     void setDumpInProgressReports(bool enable) throw() { a_dumpInProgressReports = enable; }
134     void setDumpFailedReports(bool enable) throw() { a_dumpFailedReports = enable; }
135     void setDumpSuccessReports(bool enable) throw() { a_dumpSuccessReports = enable; }
136     void setDumpAllReports(bool enable) throw() {
137       setDumpInitializedReports(enable);
138       setDumpInProgressReports(enable);
139       setDumpFailedReports(enable);
140       setDumpSuccessReports(enable);
141     }
142
143     bool getDumpInitializedReports() const throw() { return a_dumpInitializedReports; }
144     bool getDumpInProgressReports() const throw() { return a_dumpInProgressReports; }
145     bool getDumpFailedReports() const throw() { return a_dumpFailedReports; }
146     bool getDumpSuccessReports() const throw() { return a_dumpSuccessReports; }
147
148     // Helper to calculate time interval and synchronous amount of execution tests to guarantee the input rate (tests per second)
149     //  through the time manager. The first call to this method will start the time trigger system and check for new test cases to be launched.
150     bool configureTTPS(int testTicksPerSecond) throw();
151
152     bool clearPool() throw();
153     bool resetPool(bool hard /* hard reset includes in-progress test cases */) throw();
154     void setPoolRepeats(int repeats) throw() { a_poolRepeats = repeats; }
155     int getPoolRepeats() const throw() { return a_poolRepeats; }
156     int getPoolCycle() const throw() { return a_poolCycle; }
157
158     bool getAutoResetHard() const throw() { return a_autoResetHard; }
159     void setAutoResetHard(bool hard = true) throw() { a_autoResetHard = hard; }
160
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     // Diameter
171     TestCase *getDiameterTestCaseFromSessionId(const anna::DataBlock &message, std::string &sessionId) throw();
172     TestCase *getDiameterTestCaseFromSubscriberId(const anna::DataBlock &message, std::string &subscriberId) throw();
173     void receiveDiameterMessage(const anna::DataBlock &message, const anna::diameter::comm::ClientSession *clientSession) throw(anna::RuntimeException);
174     void receiveDiameterMessage(const anna::DataBlock &message, const anna::diameter::comm::ServerSession *serverSession) throw(anna::RuntimeException);
175
176     // Non-trasactional (kafka)
177     //
178     //
179
180     bool execTestCases(int sync_amount) throw();
181
182     anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
183     std::string asXMLString() const throw();
184
185     // stats
186     void tcsStateStats(const TestCase::State::_v beginState, const TestCase::State::_v endState) throw() {
187       a_statSummary.newTCState(beginState, endState);
188     }
189
190
191   friend class anna::Singleton <TestManager>;
192   friend class TestStepTimeout; // createTimer
193   friend class TestStepDelay; // createTimer
194   friend class TestClock; // tick
195 };
196
197 }
198 }
199
200 #endif
201