PhoenixSwarm  5.1.1
Library to ease communication between daemons
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include <iostream>
8#include "phoenix_assert.h"
9#include "BaseDaemon.h"
10#include "Representation.h"
11
12using namespace Swarm;
13
16 BaseDaemon daemon;
17 Message msg;
18 msg.setId(123);
19 msg.setSendTime(time(nullptr));
20 msg.getVecRecver().push_back("receiver1");
21 daemon.addMessageToConfirm(msg);
22
23 Message retrieved;
24 phoenix_assert(daemon.getMessageToConfirm(retrieved, 123));
25 phoenix_assert(retrieved.getId() == 123);
26
27 Message notFound;
28 phoenix_assert(!daemon.getMessageToConfirm(notFound, 999));
29}
30
32
34void testBaseDaemonProcessConfirmedMessage(const PPath & fileName) {
35 BaseDaemon daemon;
36 daemon.load(fileName, "main");
37
38 Message msg;
39 msg.setId(456);
40 msg.setSendTime(3lu);
41 msg.setData(Data());
42 msg.getVecRecver().push_back("receiver2");
43 daemon.addMessageToConfirm(msg);
44
45 // Confirm the message
46 daemon.processConfirmedMessage(456, 5lu);
47 Message shouldNotExist;
48 phoenix_assert(!daemon.getMessageToConfirm(shouldNotExist, 456));
49}
50
53 PString invalidTomlConfig = R"(
54[other_section]
55name = "test"
56value = 42
57
58[another_section]
59host = "localhost"
60)";
61 BaseDaemon daemon;
62 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon.load(invalidTomlConfig, "main", ConfigFormat::TOML));
63}
64
67 PString invalidYmlConfig = R"(
68other_section:
69 name: "test"
70 value: 42
71
72another_section:
73 host: "localhost"
74)";
75 BaseDaemon daemon;
76 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon.load(invalidYmlConfig, "main", ConfigFormat::YAML));
77}
78
81 PString invalidJsonConfig = R"({
82"other_section": {
83 "name": "test",
84 "value": 42
85},
86"another_section": {
87 "host": "localhost"
88}
89})";
90 BaseDaemon daemon;
91 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon.load(invalidJsonConfig, "main", ConfigFormat::JSON));
92}
93
96 PPath tempFile = PPath("test_no_daemon_section.toml");
97 PString invalidTomlConfig = R"(
98[other_section]
99name = "test"
100value = 42
101)";
102 tempFile.saveFileContent(invalidTomlConfig);
103 BaseDaemon daemon;
104 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon.load(tempFile, "main"));
105}
106
109 PPath tempFile = PPath("test_no_daemon_section.yml");
110 PString invalidYmlConfig = R"(
111other_section:
112 name: "test"
113 value: 42
114)";
115 tempFile.saveFileContent(invalidYmlConfig);
116 BaseDaemon daemon;
117 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon.load(tempFile, "main"));
118}
119
122 PPath tempFile = PPath("test_no_daemon_section.json");
123 PString invalidJsonConfig = R"({
124"other_section": {
125 "name": "test",
126 "value": 42
127}
128})";
129 tempFile.saveFileContent(invalidJsonConfig);
130 BaseDaemon daemon;
131 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon.load(tempFile, "main"));
132}
133
135
137void testBaseDaemonLoadConfigToml(const PPath & fileName){
138 BaseDaemon daemon;
139 daemon.load(fileName, "main");
140 phoenix_assert(daemon.isDaemonExist("other"));
141 BaseDaemon daemon2;
142 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon2.load(fileName, "not_in_in_the_list"));
143 BaseDaemon daemon3;
144 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ParserException, daemon3.load(PPath("unexisting_config.toml"), "main"));
145}
146
148
150void testBaseDaemonLoadConfigTomlString(const PPath & fileName){
151 // Read toml file in a string
152 PString tomlString = fileName.loadFileContent();
153 BaseDaemon daemon;
154 daemon.load(tomlString, "main", ConfigFormat::TOML);
155 phoenix_assert(daemon.isDaemonExist("other"));
156 BaseDaemon daemon2;
157 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon2.load(tomlString, "not_in_in_the_list", ConfigFormat::TOML));
158}
159
161
163void testBaseDaemonLoadConfigYml(const PPath & fileName){
164 BaseDaemon daemon;
165 daemon.load(fileName, "main");
166 phoenix_assert(daemon.isDaemonExist("other"));
167 BaseDaemon daemon2;
168 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon2.load(fileName, "not_in_in_the_list"));
169 BaseDaemon daemon3;
170 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ParserException, daemon3.load(PPath("unexisting_config.yml"), "main"));
171 BaseDaemon daemon4;
172 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ParserException, daemon4.load(PPath("file.nonextension"), "main"));
173}
174
176
178void testBaseDaemonLoadConfigYmlString(const PPath & fileName){
179 // Read yaml file in a string
180 PString ymlString = fileName.loadFileContent();
181 BaseDaemon daemon;
182 daemon.load(ymlString, "main", ConfigFormat::YAML);
183 phoenix_assert(daemon.isDaemonExist("other"));
184 BaseDaemon daemon2;
185 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon2.load(ymlString, "not_in_in_the_list", ConfigFormat::YAML));
186}
187
189
191void testBaseDaemonLoadConfigJson(const PPath & fileName){
192 BaseDaemon daemon;
193 phoenix_assert(daemon.load(fileName, "main"));
194 phoenix_assert(daemon.isDaemonExist("other"));
195 BaseDaemon daemon2;
196 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon2.load(fileName, "not_in_in_the_list"));
197 BaseDaemon daemon3;
198 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ParserException, daemon3.load(PPath("unexisting_config.json"), "main"));
199 BaseDaemon daemon4;
200 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ParserException, daemon4.load(PPath("file.nonextension"), "main"));
201}
202
204
206void testBaseDaemonLoadConfigJsonString(const PPath & fileName){
207 // Read json file in a string
208 PString jsonString = fileName.loadFileContent();
209 BaseDaemon daemon;
210 daemon.load(jsonString, "main", ConfigFormat::JSON);
211 phoenix_assert(daemon.isDaemonExist("other"));
212 BaseDaemon daemon2;
213 PHOENIX_EXCEPTION_TYPE_ASSERT(Phoenix::ConfigException, daemon2.load(jsonString, "not_in_in_the_list", ConfigFormat::JSON));
214}
215
217
220void testBaseDaemonParseArgument(const char* configFile, const char* daemonName) {
221 const char* argv[] = {
222 "phoenix_daemon",
223 "--daemonconfig", configFile,
224 "--daemonname", daemonName
225 };
226 int argc = sizeof(argv) / sizeof(argv[0]);
227 BaseDaemon daemon;
228 phoenix_assert(daemon.parseArgument(argc, const_cast<char**>(argv)));
229 daemon.stop();
230 phoenix_assert(daemon.isDaemonExist("other"));
231}
232
235 BaseDaemon daemon;
236 StatAccumulator stat;
237 stat.setHistLowerBound(0.0f);
238 stat.setHistUpperBound(10.0f);
239 stat.setVecHistogram(std::vector<size_t>(5, 0lu)); //5 bins
240
241 daemon.updateStatAccumulator(stat, 2.0f);
242 phoenix_assert(stat.getNbEvent() == 1lu);
243 phoenix_assert(stat.getMin() == 2.0f);
244 phoenix_assert(stat.getMax() == 2.0f);
245 phoenix_assert(stat.getSum() == 2.0f);
246 phoenix_assert(stat.getVecHistogram()[1] == 1lu); //bin size = 2.0, so 2.0 goes to bin 1
247
248 daemon.updateStatAccumulator(stat, 8.0f);
249 phoenix_assert(stat.getNbEvent() == 2lu);
250 phoenix_assert(stat.getMin() == 2.0f);
251 phoenix_assert(stat.getMax() == 8.0f);
252 phoenix_assert(stat.getSum() == 10.0f);
253 phoenix_assert(stat.getVecHistogram()[4] == 1lu); //8.0 goes to bin 4
254
255 daemon.updateStatAccumulator(stat, -1.0f); //below lower bound
256 phoenix_assert(stat.getNbEvent() == 3lu);
257 phoenix_assert(stat.getNbEventBelowLowerBound() == 1lu);
258
259 daemon.updateStatAccumulator(stat, 12.0f); //above upper bound
260 phoenix_assert(stat.getNbEvent() == 4lu);
261 phoenix_assert(stat.getNbEventAboveUpperBound() == 1lu);
262}
263
266 BaseDaemon daemon;
267 StatAccumulator stat;
268 stat.setHistLowerBound(0.0f);
269 stat.setHistUpperBound(10.0f);
270 stat.setVecHistogram(std::vector<size_t>(5, 0lu)); //5 bins
271
272 daemon.getConfig().getDaemonStatAccumulator().getMapStatComputing()["stat_name"] = stat;
273 daemon.getConfig().getDaemonStatAccumulator().getMapStatCommunication()["daemon_name"]["stat_name"] = stat;
274
275 phoenix_assert(!daemon.getConfig().getDaemonStatAccumulator().getMapStatComputing().empty());
276 phoenix_assert(!daemon.getConfig().getDaemonStatAccumulator().getMapStatCommunication().empty());
277
278 daemon.clearStat();
279 for(MapStatAccumulator::const_iterator it = daemon.getConfig().getDaemonStatAccumulator().getMapStatComputing().begin();
280 it != daemon.getConfig().getDaemonStatAccumulator().getMapStatComputing().end(); ++it)
281 {
282 phoenix_assert(it->second.getNbEvent() == 0lu);
283 for(std::vector<size_t>::const_iterator vit = it->second.getVecHistogram().begin();
284 vit != it->second.getVecHistogram().end(); ++vit) {
285 phoenix_assert(*vit == 0lu);
286 }
287 }
288 for(MapDaemonStatAccumulator::const_iterator itMap = daemon.getConfig().getDaemonStatAccumulator().getMapStatCommunication().begin();
289 itMap != daemon.getConfig().getDaemonStatAccumulator().getMapStatCommunication().end(); ++itMap)
290 {
291 for(MapStatAccumulator::const_iterator it = itMap->second.begin(); it != itMap->second.end(); ++it) {
292 phoenix_assert(it->second.getNbEvent() == 0lu);
293 for(std::vector<size_t>::const_iterator vit = it->second.getVecHistogram().begin();
294 vit != it->second.getVecHistogram().end(); ++vit) {
295 phoenix_assert(*vit == 0lu);
296 }
297 }
298 }
299}
300
302
305 StatAccumulator stat;
306 stat.setNbEvent(4lu);
307 stat.setMin(2.0f);
308 stat.setMax(8.0f);
309 stat.setSum(20.0f);
312 stat.setHistLowerBound(0.0f);
313 stat.setHistUpperBound(10.0f);
314 stat.setVecHistogram(std::vector<size_t>(5, 0lu));
315 stat.getVecHistogram()[1] = 1lu; //bin for 2.0
316 stat.getVecHistogram()[4] = 1lu; //bin for 8.0
317 return stat;
318}
319
322 public:
325};
326
329 TestBaseDaemon daemon;
331 time_t startTime = 1000lu;
332 time_t endTime = 2000lu;
333
334 VecStat vecStat = daemon.fillVecStat(stat, startTime, endTime);
335 phoenix_assert(vecStat.getNbEvent().size() == 1lu);
336 phoenix_assert(vecStat.getNbEvent()[0] == 4lu);
337 phoenix_assert(vecStat.getMin().size() == 1lu);
338 phoenix_assert(vecStat.getMin()[0] == 2.0f);
339 phoenix_assert(vecStat.getMax().size() == 1lu);
340 phoenix_assert(vecStat.getMax()[0] == 8.0f);
341 phoenix_assert(vecStat.getAverage().size() == 1lu);
342 phoenix_assert(vecStat.getAverage()[0] == 5.0f); //20.0 / 4
343
344 phoenix_assert(vecStat.getStartTimestamp().size() == 1lu);
345 phoenix_assert(vecStat.getStartTimestamp()[0] == startTime);
346 phoenix_assert(vecStat.getEndTimestamp().size() == 1lu);
347 phoenix_assert(vecStat.getEndTimestamp()[0] == endTime);
348 phoenix_assert(vecStat.getRate().size() == 1lu);
349 phoenix_assert(vecStat.getRateEventBelowLowerBound().size() == 1lu);
350 phoenix_assert(vecStat.getRateEventAboveUpperBound().size() == 1lu);
351 phoenix_assert(vecStat.getVecRateQuantile().size() == 5lu);
352}
353
356 TestBaseDaemon daemon;
358 daemon.getConfig().getDaemonStatAccumulator().getMapStatComputing()["stat1"] = stat1;
360 daemon.getConfig().getDaemonStatAccumulator().getMapStatCommunication()["other"]["stat2"] = stat2;
361
362 Stat daemonStat;
363 time_t startTime = 1000lu;
364 time_t endTime = 2000lu;
365 daemon.fillDaemonStat(daemonStat, startTime, endTime);
366
367 phoenix_assert(daemonStat.getMapStatComputing().find("stat1") != daemonStat.getMapStatComputing().end());
368 phoenix_assert(daemonStat.getMapStatCommunication().find("other") != daemonStat.getMapStatCommunication().end());
369 phoenix_assert(daemonStat.getMapStatCommunication()["other"].find("stat2") != daemonStat.getMapStatCommunication()["other"].end());
370 phoenix_assert(daemonStat.getMapStatComputing().find("bad_stat") == daemonStat.getMapStatComputing().end());
371}
372
373int main(int argc, char** argv){
374
378
382
383 testBaseDaemonLoadConfigToml(PPath(DAEMON_CONFIG_OK_TOML));
384 testBaseDaemonLoadConfigTomlString(PPath(DAEMON_CONFIG_OK_TOML));
385 testBaseDaemonLoadConfigYml(PPath(DAEMON_CONFIG_OK_YAML));
386 testBaseDaemonLoadConfigYmlString(PPath(DAEMON_CONFIG_OK_YAML));
387 testBaseDaemonLoadConfigJson(PPath(DAEMON_CONFIG_OK_JSON));
388 testBaseDaemonLoadConfigJsonString(PPath(DAEMON_CONFIG_OK_JSON));
389 testBaseDaemonParseArgument(DAEMON_CONFIG_OK_TOML, "main");
390 testBaseDaemonProcessConfirmedMessage(PPath(DAEMON_CONFIG_OK_TOML));
396 return 0;
397}
398
void testBaseDaemonGetMessageToConfirm()
Get a BaseDaemon with a message to confirm.
Definition main.cpp:15
void testBaseDaemonLoadConfigYml(const PPath &fileName)
Test the load configuration of a BaseDaemon.
Definition main.cpp:163
void testBaseDaemonLoadConfigJson(const PPath &fileName)
Test the load configuration of a BaseDaemon.
Definition main.cpp:191
void testBaseDaemonMissingDaemonSectionTomlString()
Test BaseDaemon with missing daemon section in TOML string config.
Definition main.cpp:52
int main(int argc, char **argv)
Definition main.cpp:373
void testFillVecStat()
Test the fill vector of stats.
Definition main.cpp:328
void testBaseDaemonParseArgument(const char *configFile, const char *daemonName)
Test the parse argument of a BaseDaemon.
Definition main.cpp:220
void testUpdateStatAccumulator()
Test the update of a StatAccumulator.
Definition main.cpp:234
void testFillDaemonStat()
Test the fill of Daemon stats.
Definition main.cpp:355
void testBaseDaemonMissingDaemonSectionYmlString()
Test BaseDaemon with missing daemon section in YAML string config.
Definition main.cpp:66
void testBaseDaemonLoadConfigToml(const PPath &fileName)
Test the load configuration of a BaseDaemon.
Definition main.cpp:137
void testBaseDaemonProcessConfirmedMessage(const PPath &fileName)
Test BaseDaemon with a message to process.
Definition main.cpp:34
void testBaseDaemonLoadConfigYmlString(const PPath &fileName)
Test the load configuration of a BaseDaemon from a string YAML.
Definition main.cpp:178
void testBaseDaemonMissingDaemonSectionTomlFile()
Test BaseDaemon with missing daemon section in TOML file.
Definition main.cpp:95
void testBaseDaemonLoadConfigJsonString(const PPath &fileName)
Test the load configuration of a BaseDaemon from a JSON string.
Definition main.cpp:206
StatAccumulator createTestStatAccumulator()
Create stat accumulator.
Definition main.cpp:304
void testBaseDaemonMissingDaemonSectionJsonFile()
Test BaseDaemon with missing daemon section in JSON file.
Definition main.cpp:121
void testClearStatAccumulator()
Test the clear of StatAccumulator in BaseDaemon.
Definition main.cpp:265
void testBaseDaemonMissingDaemonSectionJsonString()
Test BaseDaemon with missing daemon section in JSON string config.
Definition main.cpp:80
void testBaseDaemonMissingDaemonSectionYmlFile()
Test BaseDaemon with missing daemon section in YML file.
Definition main.cpp:108
void testBaseDaemonLoadConfigTomlString(const PPath &fileName)
Test the load configuration of a BaseDaemon from a string TOML.
Definition main.cpp:150
Exception for daemon configuration errors.
Exception for configuration errors.
Daemon which help communication between processes and thread.
Definition BaseDaemon.h:54
bool isDaemonExist(const PString &name) const
Say if a neighbour Daemon does exist.
void updateStatAccumulator(StatAccumulator &stat, float value)
Update a computing statistic with a new value.
void addMessageToConfirm(const Swarm::Message &message)
Add a message to confirm.
bool load(const PString &configFileContent, const PString &daemonName, ConfigFormat::ConfigFormat format)
Load the Json configuration which define all BaseDaemons of the Swarm.
DaemonConfig & getConfig()
Get the configuration of the current BaseDaemon.
VecStat fillVecStat(const Swarm::StatAccumulator &accumulator, time_t startTimestamp, time_t endTimestamp)
Fill a VecStat from a StatAccumulator to send to the DamonStat.
void fillDaemonStat(Swarm::Stat &stat, time_t startTimestamp, time_t endTimestamp)
Fill the Stat with the current statistics of the daemon.
bool getMessageToConfirm(Swarm::Message &message, size_t id) const
Get a message to confirm by id if it exists.
void clearStat()
Clear all the statistics of the daemon.
void stop()
Stops the BaseDaemon.
BaseDaemon()
Default constructor of BaseDaemon.
void processConfirmedMessage(size_t id, time_t currentTime)
Process confirmed message.
bool parseArgument(int argc, char **argv)
Parse arguments given to the BaseDaemon with command line.
const Swarm::DaemonStatAccumulator & getDaemonStatAccumulator() const
Gets the daemonStatAccumulator of the DaemonConfig.
const std::map< PString, Swarm::StatAccumulator > & getMapStatComputing() const
Gets the mapStatComputing of the DaemonStatAccumulator.
const std::map< DaemonName, std::map< DataType, Swarm::StatAccumulator > > & getMapStatCommunication() const
Gets the mapStatCommunication of the DaemonStatAccumulator.
Basic Data exchanged in the swarm.
Message exchanged by Daemons.
void setData(const Swarm::Data &data)
Sets the data of the Message.
const std::vector< PString > & getVecRecver() const
Gets the vecRecver of the Message.
void setSendTime(const time_t &sendTime)
Sets the sendTime of the Message.
size_t getId() const
Gets the id of the Message.
void setId(size_t id)
Sets the id of the Message.
Accumulator of event occurence to build swarm statistics over a time period.
void setMin(float min)
Sets the min of the StatAccumulator.
float getSum() const
Gets the sum of the StatAccumulator.
void setVecHistogram(const std::vector< size_t > &vecHistogram)
Sets the vecHistogram of the StatAccumulator.
size_t getNbEvent() const
Gets the nbEvent of the StatAccumulator.
float getMax() const
Gets the max of the StatAccumulator.
size_t getNbEventAboveUpperBound() const
Gets the nbEventAboveUpperBound of the StatAccumulator.
float getMin() const
Gets the min of the StatAccumulator.
void setHistUpperBound(float histUpperBound)
Sets the histUpperBound of the StatAccumulator.
void setMax(float max)
Sets the max of the StatAccumulator.
void setNbEventAboveUpperBound(size_t nbEventAboveUpperBound)
Sets the nbEventAboveUpperBound of the StatAccumulator.
void setHistLowerBound(float histLowerBound)
Sets the histLowerBound of the StatAccumulator.
void setNbEventBelowLowerBound(size_t nbEventBelowLowerBound)
Sets the nbEventBelowLowerBound of the StatAccumulator.
void setSum(float sum)
Sets the sum of the StatAccumulator.
size_t getNbEventBelowLowerBound() const
Gets the nbEventBelowLowerBound of the StatAccumulator.
const std::vector< size_t > & getVecHistogram() const
Gets the vecHistogram of the StatAccumulator.
void setNbEvent(size_t nbEvent)
Sets the nbEvent of the StatAccumulator.
Statistics of a Daemon.
const std::map< DaemonName, std::map< DataType, Swarm::VecStat > > & getMapStatCommunication() const
Gets the mapStatCommunication of the Stat.
const std::map< PString, Swarm::VecStat > & getMapStatComputing() const
Gets the mapStatComputing of the Stat.
General statistics in the swarm.
const std::vector< float > & getMax() const
Gets the max of the VecStat.
const std::vector< time_t > & getEndTimestamp() const
Gets the endTimestamp of the VecStat.
const std::vector< float > & getRate() const
Gets the rate of the VecStat.
const std::vector< time_t > & getStartTimestamp() const
Gets the startTimestamp of the VecStat.
const std::vector< float > & getAverage() const
Gets the average of the VecStat.
const std::vector< float > & getRateEventBelowLowerBound() const
Gets the rateEventBelowLowerBound of the VecStat.
const std::vector< float > & getRateEventAboveUpperBound() const
Gets the rateEventAboveUpperBound of the VecStat.
const std::vector< float > & getMin() const
Gets the min of the VecStat.
const std::vector< size_t > & getNbEvent() const
Gets the nbEvent of the VecStat.
const std::vector< std::vector< float > > & getVecRateQuantile() const
Gets the vecRateQuantile of the VecStat.
Test Daemon.
Definition main.cpp:321
VecStat fillVecStat(const Swarm::StatAccumulator &accumulator, time_t startTimestamp, time_t endTimestamp)
Fill a VecStat from a StatAccumulator to send to the DamonStat.
void fillDaemonStat(Swarm::Stat &stat, time_t startTimestamp, time_t endTimestamp)
Fill the Stat with the current statistics of the daemon.