Skip to content

Commit 0596f61

Browse files
Refactor some asserts to conditional expressions (#238)
* Refactor some asserts to conditional expressions Signed-off-by: Raul Sanchez-Mateos <[email protected]> * Fix build issue Signed-off-by: JesusPoderoso <[email protected]> * Refactor substraction operation Signed-off-by: JesusPoderoso <[email protected]> * Refs #21681: Remove death asserts in tests Signed-off-by: JesusPoderoso <[email protected]> --------- Signed-off-by: Raul Sanchez-Mateos <[email protected]> Signed-off-by: JesusPoderoso <[email protected]> Co-authored-by: JesusPoderoso <[email protected]>
1 parent 751e61a commit 0596f61

File tree

8 files changed

+59
-408
lines changed

8 files changed

+59
-408
lines changed

include/fastdds_statistics_backend/listener/DomainListener.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ class FASTDDS_STATISTICS_BACKEND_DllAPI DomainListener
7575

7676
void on_instance_undiscovered()
7777
{
78-
assert (current_count > 0);
79-
--current_count;
80-
--current_count_change;
78+
if (current_count > 0)
79+
{
80+
--current_count;
81+
--current_count_change;
82+
}
8183
}
8284

8385
void on_status_read()

src/cpp/StatisticsBackendData.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
2626
#include <fastdds/dds/domain/qos/DomainParticipantFactoryQos.hpp>
2727
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
28+
#include <fastdds/dds/log/Log.hpp>
2829
#include <fastdds/dds/subscriber/DataReader.hpp>
2930
#include <fastdds/dds/subscriber/Subscriber.hpp>
3031
#include <fastdds/dds/topic/Topic.hpp>
@@ -122,7 +123,11 @@ void StatisticsBackendData::on_data_available(
122123
DataKind data_kind)
123124
{
124125
auto monitor = monitors_by_entity_.find(domain_id);
125-
assert(monitor != monitors_by_entity_.end());
126+
if (monitor == monitors_by_entity_.end())
127+
{
128+
logWarning(STATISTICS_BACKEND_DATA, "Monitor not found for domain " << domain_id);
129+
return;
130+
}
126131

127132
if (should_call_domain_listener(*monitor->second, CallbackKind::ON_DATA_AVAILABLE, data_kind))
128133
{
@@ -140,7 +145,11 @@ void StatisticsBackendData::on_status_reported(
140145
StatusKind status_kind)
141146
{
142147
auto monitor = monitors_by_entity_.find(domain_id);
143-
assert(monitor != monitors_by_entity_.end());
148+
if (monitor == monitors_by_entity_.end())
149+
{
150+
logWarning(STATISTICS_BACKEND_DATA, "Monitor not found for domain " << domain_id);
151+
return;
152+
}
144153

145154
if (should_call_domain_listener(*monitor->second, CallbackKind::ON_STATUS_REPORTED))
146155
{
@@ -228,7 +237,12 @@ void StatisticsBackendData::on_domain_entity_discovery(
228237
DiscoveryStatus discovery_status)
229238
{
230239
auto monitor = monitors_by_entity_.find(domain_id);
231-
assert(monitor != monitors_by_entity_.end());
240+
if (monitor == monitors_by_entity_.end())
241+
{
242+
logWarning(STATISTICS_BACKEND_DATA, "Monitor not found for domain " << domain_id);
243+
return;
244+
}
245+
232246
switch (entity_kind)
233247
{
234248
case EntityKind::PARTICIPANT:
@@ -306,7 +320,7 @@ void StatisticsBackendData::on_domain_entity_discovery(
306320
}
307321
default:
308322
{
309-
assert(false && "Invalid domain entity kind");
323+
logWarning(STATISTICS_BACKEND, "Invalid domain entity kind");
310324
}
311325
}
312326
}
@@ -316,7 +330,11 @@ void StatisticsBackendData::on_physical_entity_discovery(
316330
EntityKind entity_kind,
317331
DiscoveryStatus discovery_status)
318332
{
319-
assert(discovery_status != DiscoveryStatus::UPDATE);
333+
if (discovery_status == DiscoveryStatus::UPDATE)
334+
{
335+
logWarning(STATISTICS_BACKEND, "UPDATE discovery status is not supported for physical entities");
336+
return;
337+
}
320338

321339
switch (entity_kind)
322340
{
@@ -370,7 +388,7 @@ void StatisticsBackendData::on_physical_entity_discovery(
370388
}
371389
default:
372390
{
373-
assert(false && "Invalid physical entity kind");
391+
logWarning(STATISTICS_BACKEND, "Invalid physical entity kind");
374392
}
375393
}
376394
}

src/cpp/database/database_queue.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ EntityId DatabaseEntityQueue::process_datareader(
187187
try
188188
{
189189
participant_id = database_->get_entity_by_guid(EntityKind::PARTICIPANT, to_string(participant_guid));
190-
assert(participant_id.first == info.domain_id);
190+
if (participant_id.first != info.domain_id)
191+
{
192+
throw BadParameter("Participant " + to_string(participant_guid)
193+
+ " found in the database but it is not in the current domain");
194+
}
191195
}
192196
catch (const Exception&)
193197
{
@@ -258,7 +262,11 @@ EntityId DatabaseEntityQueue::process_datawriter(
258262
try
259263
{
260264
participant_id = database_->get_entity_by_guid(EntityKind::PARTICIPANT, to_string(participant_guid));
261-
assert(participant_id.first == info.domain_id);
265+
if (participant_id.first != info.domain_id)
266+
{
267+
throw BadParameter("Participant " + to_string(participant_guid)
268+
+ " found in the database but it is not in the current domain");
269+
}
262270
}
263271
catch (const Exception&)
264272
{
@@ -317,7 +325,11 @@ EntityId DatabaseEntityQueue::process_endpoint_discovery(
317325
try
318326
{
319327
participant_id = database_->get_entity_by_guid(EntityKind::PARTICIPANT, to_string(participant_guid));
320-
assert(participant_id.first == info.domain_id);
328+
if (participant_id.first != info.domain_id)
329+
{
330+
throw BadParameter("Participant " + to_string(participant_guid)
331+
+ " found in the database but it is not in the current domain");
332+
}
321333
}
322334
catch (const Exception&)
323335
{

src/cpp/database/samples.hpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <chrono>
2424

25+
#include <fastdds/dds/log/Log.hpp>
2526
#include <fastdds_statistics_backend/exception/Exception.hpp>
2627
#include <fastdds_statistics_backend/types/types.hpp>
2728

@@ -122,10 +123,17 @@ struct EntityCountSample : StatisticsSample
122123
inline EntityCountSample operator -(
123124
const EntityCountSample& other) const noexcept
124125
{
125-
assert(count >= other.count);
126126
EntityCountSample ret(kind);
127127
ret.src_ts = src_ts;
128-
ret.count = count - other.count;
128+
if (count >= other.count)
129+
{
130+
ret.count = count - other.count;
131+
}
132+
else
133+
{
134+
logWarning(STATISTICS_BACKEND, "Subtraction of EntityCountSample resulted in a negative number");
135+
ret.count = 0;
136+
}
129137
return ret;
130138
}
131139

test/unittest/Database/SampleTests.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ TEST(database, entitycountsample_operator_comparison)
109109
ASSERT_NE(sample_1, sample_2);
110110
}
111111

112+
// updated to avoid unnecessary asserts in code, check #21681 Redmine task
112113
TEST(database, entitycountsample_operator_minus)
113114
{
114115
EntityCountSample sample_1;
@@ -125,11 +126,11 @@ TEST(database, entitycountsample_operator_minus)
125126
ASSERT_EQ(sample_3.count, 1u);
126127
ASSERT_EQ(sample_3.src_ts, sample_1.src_ts);
127128

128-
#ifndef NDEBUG
129-
// Assertion are active here
129+
// If negative number, subtraction returns 0
130130
sample_2.count = 3;
131-
ASSERT_DEATH(sample_1 - sample_2, "");
132-
#endif // ifndef NDEBUG
131+
sample_3 = sample_1 - sample_2;
132+
ASSERT_EQ(sample_3.count, 0u);
133+
ASSERT_EQ(sample_3.src_ts, sample_1.src_ts);
133134
}
134135

135136
TEST(database, bytecountsample_operator_comparison)

test/unittest/StatisticsBackend/CMakeLists.txt

-21
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ set(BACKEND_TEST_LIST
7070
get_type
7171
get_data_supported_entity_kinds
7272
set_alias
73-
internal_callbacks_negative_cases
7473
set_listener_non_existent_monitor
7574
get_domain_view_graph
7675
get_domain_view_graph_invalid_domain
@@ -178,26 +177,6 @@ foreach(test_name ${USER_LISTENERS_TEST_LIST})
178177

179178
endforeach()
180179

181-
set(USER_LISTENERS_DEATH_TEST_LIST
182-
wrong_entity_kind
183-
)
184-
185-
foreach(test_name ${USER_LISTENERS_DEATH_TEST_LIST})
186-
add_test(NAME calling_user_listeners_DeathTest.${test_name}
187-
COMMAND calling_user_listeners_tests
188-
--gtest_filter=calling_user_listeners_DeathTest.${test_name}:*/calling_user_listeners_DeathTest.${test_name}/*)
189-
190-
if(TEST_FRIENDLY_PATH)
191-
set_tests_properties(calling_user_listeners_DeathTest.${test_name}
192-
PROPERTIES
193-
ENVIRONMENT "PATH=${TEST_FRIENDLY_PATH}"
194-
)
195-
endif(TEST_FRIENDLY_PATH)
196-
endforeach()
197-
198-
# Add xfail label to failing test
199-
set_property(TEST calling_user_listeners_DeathTest.wrong_entity_kind PROPERTY LABELS xfail)
200-
201180
set(USER_LISTENERS_TEST_LIST_PHYSICAL_ENTITIES
202181
entity_discovered
203182
entity_discovered_not_in_mask

0 commit comments

Comments
 (0)