SledgeHammerTest.cpp
6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/****************************************************************************
* COpyright (c) 2023 Open Systems Development B.V.
****************************************************************************/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <memory>
#include <chrono>
#include <unistd.h>
#include <iostream>
#include "helperclasses/PublisherClass.h"
/// Every test does basically the same.
/// 1. Create a Publisher object
/// 2. Connect to the MQTT broker
/// 3. Publish 10 message(s)
/// 4. Disconnect from the MQTT broker
/// 5. Destroy the Publisher object
/// 6. Repeat 10 times.
const std::string sledge_maintopic = "SledgeHammerTest/";
enum TIME_RES
{
T_MICRO,
T_MILLI,
T_SECONDS
};
std::uint64_t getEpochUSecs()
{
auto tsUSec =std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
return static_cast<std::uint64_t>(tsUSec.time_since_epoch().count());
}
void sleepcp( int number, TIME_RES resolution = T_MILLI ) // Cross-platform sleep function
{
int factor = 0; // Should not happen..
switch( resolution )
{
case T_MICRO:
factor = 1;
break;
case T_MILLI:
factor = 1000;
break;
case T_SECONDS:
factor = 1000000;
break;
}
usleep( number * factor );
}
/// Test a single connection, 1 time
TEST(SledgeHammerTest, SingleConnectionCleanExit)
{
Publisher *publisher = nullptr;
for (int test_counter = 0; test_counter < 10; test_counter++)
{
publisher = new Publisher(std::to_string(getEpochUSecs()));
if(publisher)
{
publisher->connect("localhost");
publisher->publish(sledge_maintopic + "Single Connection Clean Exit Test/" + std::to_string(test_counter), "Hello World. I'm alive..");
publisher->disconnect();
delete publisher;
publisher = nullptr;
}
}
}
TEST(SledgeHammerTest, SingleConnectionForcedExit)
{
Publisher *publisher = nullptr;
for (int test_counter = 0; test_counter < 10; test_counter++)
{
publisher = new Publisher(std::to_string(getEpochUSecs()));
if(publisher)
{
publisher->connect("localhost");
publisher->publish(sledge_maintopic + "Single Connection Forced Exit/Test/" + std::to_string(test_counter), "Hello World. I'm alive..");
delete publisher;
publisher = nullptr;
}
}
}
TEST(SledgeHammerTest, MultipleConnections_10_CleanExit)
{
std::unordered_map<std::string, Publisher *> publishers;
for (int test_counter = 0; test_counter < 10; test_counter++)
{
publishers["Publisher" + std::to_string(test_counter)] = new Publisher(std::to_string(getEpochUSecs()));
if(publishers["Publisher" + std::to_string(test_counter)])
{
publishers["Publisher" + std::to_string(test_counter)]->connect("localhost");
}
}
for (int test_counter = 0; test_counter < 10; test_counter++)
{
for (int nCount = 0; nCount < 10; nCount++)
{
publishers["Publisher" + std::to_string(test_counter)]->publish(sledge_maintopic + "Multiple Connections [10]/Clean Exit/Test " + std::to_string(test_counter), "Hello World. I'm alive..");
}
publishers["Publisher" + std::to_string(test_counter)]->disconnect();
delete publishers["Publisher" + std::to_string(test_counter)];
publishers.erase("Publisher" + std::to_string(test_counter));
}
}
TEST(SledgeHammerTest, MultipleConnections_BurnTest)
{
std::unordered_map<std::string, Publisher *> publishers;
const int max_run = 100;
const int max_connections = 5;
const int max_messages = 100;
for (int test_run = 0; test_run < max_run; test_run++)
{
std::cout << "Creating " << max_connections << " connections for test run " << test_run << " " << std::endl;
for (int test_counter = 0; test_counter < max_connections; test_counter++)
{
publishers["Publisher" + std::to_string(test_counter)] = new Publisher(std::to_string(getEpochUSecs()));
if(publishers["Publisher" + std::to_string(test_counter)])
{
publishers["Publisher" + std::to_string(test_counter)]->connect("localhost");
std::cout << ".";
}
}
std::cout << std::endl;
// Wait a second for the connections to be established.
sleepcp(1, T_SECONDS);
std::cout << "Publishing " << max_messages << " messages for test run " << test_run << std::endl;
for (int test_counter = 0; test_counter < max_connections; test_counter++)
{
std::cout << "\tConnection : " << test_counter << " ";
for (int nCount = 0; nCount < max_messages; nCount++)
{
if(publishers["Publisher" + std::to_string(test_counter)])
{
std::cout << ".";
publishers["Publisher" + std::to_string(test_counter)]->publish(sledge_maintopic +
"Multiple Connections [" + std::to_string(max_connections) + "]/Multi Messages [" + std::to_string(max_messages) + "]/Test Run " +
std::to_string(test_run) + "/" + "[" + std::to_string(test_counter) + "]" +
"[" + std::to_string(nCount) + "]", "Hello World. I'm alive..");
}
}
std::cout << std::endl;
}
// Wait another second to update the broker.
sleepcp(1, T_SECONDS);
std::cout << "Disconnecting " << max_connections << " connections for test run " << test_run << " ";
for (int test_counter = 0; test_counter < max_connections; test_counter++)
{
if(publishers["Publisher" + std::to_string(test_counter)])
{
std::cout << ".";
publishers["Publisher" + std::to_string(test_counter)]->disconnect();
delete publishers["Publisher" + std::to_string(test_counter)];
}
}
std::cout << std::endl;
publishers.clear();
std::cout << std::string(200,'=') << std::endl;
// Wait for another second before re-running another 100 times.
sleepcp(1, T_SECONDS);
}
}