-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDatabaseClearTests.cpp
More file actions
256 lines (223 loc) · 9.95 KB
/
Copy pathDatabaseClearTests.cpp
File metadata and controls
256 lines (223 loc) · 9.95 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <chrono>
#include <fstream>
#include <memory>
#include <string>
#include <gtest_aux.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <fastdds_statistics_backend/exception/Exception.hpp>
#include <fastdds_statistics_backend/types/EntityId.hpp>
#include <fastdds_statistics_backend/types/JSONTags.h>
#include <fastdds_statistics_backend/types/types.hpp>
#include <StatisticsBackendData.hpp> // Mock
#include <database/database.hpp>
#include <database/entities.hpp>
#include <database/samples.hpp>
using namespace eprosima::statistics_backend;
using namespace eprosima::statistics_backend::database;
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::Throw;
using ::testing::AnyNumber;
using ::testing::StrictMock;
constexpr const char* GUID_DEFAULT = "01.0f.00.00.00.00.00.00.00.00.00.00|0.0.0.0";
constexpr const char* PID_DEFAULT = "36000";
constexpr const char* DATA_TYPE_DEFAULT = "data_type";
#define QOS_DEFAULT Qos(R"({"qos":"empty"})"_json)
constexpr const bool STATUS_DEFAULT = true;
constexpr const uint64_t COUNT_DEFAULT = 2;
constexpr const double DATA_DEFAULT = 1.1;
constexpr const uint64_t SEQUENCE_NUMBER_DEFAULT = 3;
constexpr const int16_t MAGNITUDE_DEFAULT = 0;
#define LOCATOR_DEFAULT_NAME(x) "locator_" + std::to_string(x)
#define HOST_DEFAULT_NAME(x) "host_" + std::to_string(x)
#define USER_DEFAULT_NAME(x) "user_" + std::to_string(x)
#define PROCESS_DEFAULT_NAME(x) "process_" + std::to_string(x)
#define DOMAIN_DEFAULT_NAME(x) "12" + std::to_string(x)
#define ALIAS_DEFAULT_NAME(x) "domain_" + std::to_string(x)
#define TOPIC_DEFAULT_NAME(x) "topic_" + std::to_string(x)
#define PARTICIPANT_DEFAULT_NAME(x) "participant_" + std::to_string(x)
#define DATAWRITER_DEFAULT_NAME(x) "datawriter_" + std::to_string(x)
#define DATAREADER_DEFAULT_NAME(x) "datareader_" + std::to_string(x)
#define LOCATOR_DEFAULT_ID(x) EntityId(x * 9 + 0)
#define HOST_DEFAULT_ID(x) EntityId(x * 9 + 1)
#define USER_DEFAULT_ID(x) EntityId(x * 9 + 2)
#define PROCESS_DEFAULT_ID(x) EntityId(x * 9 + 3)
#define DOMAIN_DEFAULT_ID(x) EntityId(x * 9 + 4)
#define TOPIC_DEFAULT_ID(x) EntityId(x * 9 + 5)
#define PARTICIPANT_DEFAULT_ID(x) EntityId(x * 9 + 6)
#define DATAWRITER_DEFAULT_ID(x) EntityId(x * 9 + 7)
#define DATAREADER_DEFAULT_ID(x) EntityId(x * 9 + 8)
// at least pass microseconds tenths to avoid windows system_clock resolution issue
#define TIME_DEFAULT(x) nanoseconds_to_systemclock(100 * (x))
#define PART_GUID_DEFAULT(x) "01.0f.00.00.00.00.00.00.00.00.00.0" + std::to_string(x) + "|0.0.1.0"
#define GUID_DEFAULT(x) "01.0f.00.00.00.00.00.00.00.00.00.0" + std::to_string(x) + "|0.0.0.0"
namespace test {
void initialize_empty_entities(
Database& db,
int index,
bool link_process_participant)
{
std::shared_ptr<Host> host = std::make_shared<Host>(std::string(HOST_DEFAULT_NAME(index)));
std::shared_ptr<User> user = std::make_shared<User>(std::string(USER_DEFAULT_NAME(index)), host);
std::shared_ptr<Process> process = std::make_shared<Process>(std::string(PROCESS_DEFAULT_NAME(
index)), PID_DEFAULT, user);
std::shared_ptr<Domain> domain = std::make_shared<Domain>(std::string(DOMAIN_DEFAULT_NAME(index)));
domain->alias = ALIAS_DEFAULT_NAME(index);
std::shared_ptr<Topic> topic = std::make_shared<Topic>(std::string(TOPIC_DEFAULT_NAME(
index)), DATA_TYPE_DEFAULT, domain);
std::shared_ptr<DomainParticipant> participant = std::make_shared<DomainParticipant>(std::string(
PARTICIPANT_DEFAULT_NAME(index)), QOS_DEFAULT, PART_GUID_DEFAULT(index), nullptr, domain);
std::shared_ptr<DataWriter> dw = std::make_shared<DataWriter>(std::string(
DATAWRITER_DEFAULT_NAME(index)), QOS_DEFAULT, GUID_DEFAULT(index), participant, topic);
std::shared_ptr<DataReader> dr = std::make_shared<DataReader>(std::string(
DATAREADER_DEFAULT_NAME(index)), QOS_DEFAULT, GUID_DEFAULT(index), participant, topic);
std::shared_ptr<Locator> locator = std::make_shared<Locator>(std::string(LOCATOR_DEFAULT_NAME(index)));
locator->id = db.insert(locator);
ASSERT_NE(locator->id, EntityId::invalid());
dw->locators[locator->id] = locator;
dr->locators[locator->id] = locator;
ASSERT_NE(db.insert(host), EntityId::invalid());
ASSERT_NE(db.insert(user), EntityId::invalid());
ASSERT_NE(db.insert(process), EntityId::invalid());
ASSERT_NE(db.insert(domain), EntityId::invalid());
ASSERT_NE(db.insert(topic), EntityId::invalid());
ASSERT_NE(db.insert(participant), EntityId::invalid());
ASSERT_NE(db.insert(dw), EntityId::invalid());
ASSERT_NE(db.insert(dr), EntityId::invalid());
if (link_process_participant)
{
db.link_participant_with_process(participant->id, process->id);
}
locator->data_writers[dw->id] = dw;
locator->data_readers[dr->id] = dr;
}
} // namespace test
/**
* @brief Test that clearing inactive entities from database works
*
* STEPS:
* - initialize database
* - DataWriter
* - check that has access to DataWriter
* - set DataWriter as inactive
* - clear inactive entities
* - check that DataWriter no longer exist
* - check that other entities exist and are alive
* - All Entities
* - check that has access to every entity (Host & Participant)
* - set Participant as inactive
* - clear inactive entities
* - check that all entities have been cleared (except Domain and Locators)
*/
TEST(database, clear_inactive_entities_database_simple)
{
// Expect that discovery callbacks are called with any arguments any number ot times
EXPECT_CALL(
*eprosima::statistics_backend::details::StatisticsBackendData::get_instance(),
on_physical_entity_discovery(
_, _, _)).Times(AnyNumber());
EXPECT_CALL(
*eprosima::statistics_backend::details::StatisticsBackendData::get_instance(),
on_domain_entity_discovery(
_, _, _, _)).Times(AnyNumber());
// initialize database
Database db;
test::initialize_empty_entities(db, 0, true);
// DATAWRITER
{
// check that has access to DataWriter
auto datawriters = db.get_entity_ids(EntityKind::DATAWRITER, EntityId::all());
ASSERT_EQ(datawriters.size(), 1u);
auto datawriter_id = datawriters[0];
ASSERT_NE(datawriter_id, EntityId::invalid());
ASSERT_TRUE(db.get_entity(datawriter_id)->active);
// set DataWriter as inactive
db.change_entity_status(datawriter_id, false);
ASSERT_FALSE(db.get_entity(datawriter_id)->active);
// clear inactive entities
db.clear_inactive_entities();
// check that DataWriter no longer exist
ASSERT_EQ(db.get_entity_ids(EntityKind::DATAWRITER, EntityId::all()).size(), 0u);
ASSERT_FALSE(db.is_entity_present(datawriter_id));
// check that other entities exist and are alive
std::vector<EntityKind> entities_kind_to_check = {
EntityKind::PARTICIPANT,
EntityKind::DATAREADER,
EntityKind::HOST,
EntityKind::USER,
EntityKind::PROCESS,
EntityKind::DOMAIN_ENTITY,
EntityKind::TOPIC
};
for (const auto& kind : entities_kind_to_check)
{
auto ids = db.get_entity_ids(kind, EntityId::all());
ASSERT_EQ(ids.size(), 1u);
ASSERT_TRUE(db.get_entity(ids[0])->active);
}
}
// HOST && PARTICIPANT
{
// check that has access to every entity
// Host
auto hosts = db.get_entity_ids(EntityKind::HOST, EntityId::all());
ASSERT_EQ(hosts.size(), 1u);
auto host_id = hosts[0];
ASSERT_NE(host_id, EntityId::invalid());
ASSERT_TRUE(db.get_entity(host_id)->active);
// Participant
auto participants = db.get_entity_ids(EntityKind::PARTICIPANT, EntityId::all());
ASSERT_EQ(participants.size(), 1u);
auto participant_id = participants[0];
ASSERT_NE(participant_id, EntityId::invalid());
ASSERT_TRUE(db.get_entity(participant_id)->active);
// set Participant as inactive
db.change_entity_status(participant_id, false);
ASSERT_FALSE(db.get_entity(participant_id)->active);
ASSERT_FALSE(db.get_entity(host_id)->active);
// clear inactive entities
db.clear_inactive_entities();
// check that all entities have been cleared (except for locators)
ASSERT_FALSE(db.is_entity_present(participant_id));
ASSERT_FALSE(db.is_entity_present(host_id));
std::vector<EntityKind> entities_kind_to_check = {
EntityKind::PARTICIPANT,
EntityKind::DATAWRITER,
EntityKind::DATAREADER,
EntityKind::HOST,
EntityKind::USER,
EntityKind::PROCESS,
EntityKind::TOPIC
};
for (const auto& kind : entities_kind_to_check)
{
ASSERT_EQ(db.get_entity_ids(kind, EntityId::all()).size(), 0u)
<< static_cast<int>(kind);
}
// 2 entities remain: Domain and 1 locator
ASSERT_EQ(db.get_entity_ids(EntityKind::DOMAIN_ENTITY, EntityId::all()).size(), 1u);
ASSERT_EQ(db.get_entity_ids(EntityKind::LOCATOR, EntityId::all()).size(), 1u);
}
}
int main(
int argc,
char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}