Skip to content

Commit d538426

Browse files
Improve some tests (#6167)
* Refs #23732. Use cmake wrapper for `TwoPublishersCommunicationReliable` Signed-off-by: Miguel Company <[email protected]> * Refs #23732. Improve RefCountedPointerTests. Signed-off-by: Miguel Company <[email protected]> --------- Signed-off-by: Miguel Company <[email protected]>
1 parent 5cc618c commit d538426

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

test/dds/communication/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ if(Python3_Interpreter_FOUND)
298298
endif()
299299

300300
add_test(NAME TwoPublishersCommunicationReliable
301-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/two_publishers_communication.py)
301+
COMMAND ${CMAKE_COMMAND} -DACTUAL_TEST=${PYTHON_EXECUTABLE} -DACTUAL_ARGS=${CMAKE_CURRENT_BINARY_DIR}/two_publishers_communication.py -P ${CMAKE_CURRENT_BINARY_DIR}/test_wrapper.cmake)
302302

303303
# Set test with label NoMemoryCheck
304304
set_property(TEST TwoPublishersCommunicationReliable PROPERTY LABELS "NoMemoryCheck")

test/unittest/utils/RefCountedPointerTests.cpp

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <gtest/gtest.h>
1616

1717
#include <atomic>
18+
#include <chrono>
19+
#include <condition_variable>
20+
#include <memory>
21+
#include <mutex>
1822
#include <thread>
1923
#include <vector>
2024

@@ -63,18 +67,30 @@ enum class RoutineStatus
6367

6468
struct EntityOwner
6569
{
70+
struct SyncPoint
71+
{
72+
virtual void notify_and_wait() = 0;
73+
};
74+
6675
EntityOwner(
6776
const EntityMock& entity)
6877
: entity_ptr(entity.get_refcounter_pointer())
6978
, routine_status(RoutineStatus::NON_INITIALIZED)
7079
{
7180
}
7281

73-
void spawn_routine()
82+
void spawn_routine(
83+
SyncPoint* sync = nullptr)
7484
{
75-
th = std::thread([&]()
85+
th = std::thread([this, sync]()
7686
{
7787
RefCountedPointer<EntityMock>::Instance entity_instance(entity_ptr);
88+
89+
if (sync != nullptr)
90+
{
91+
sync->notify_and_wait();
92+
}
93+
7894
if (entity_instance)
7995
{
8096
entity_instance->dummy_process_data(nullptr);
@@ -105,7 +121,7 @@ class RefCountedPointerTests : public ::testing::Test
105121

106122
void SetUp() override
107123
{
108-
owners_.reserve(5);
124+
owners_.reserve(n_owners);
109125
for (std::size_t i = 0; i < n_owners; ++i)
110126
{
111127
owners_.emplace_back(entity_);
@@ -153,21 +169,61 @@ TEST_F(RefCountedPointerTests, refcountedpointer_inactive)
153169

154170
TEST_F(RefCountedPointerTests, refcounterpointer_deactivate_waits_for_no_references)
155171
{
172+
struct WaitForAllOwners : public EntityOwner::SyncPoint
173+
{
174+
WaitForAllOwners()
175+
: num_notifications_(0)
176+
{
177+
}
178+
179+
void notify_and_wait() override
180+
{
181+
std::unique_lock<std::mutex> lock(mutex_);
182+
++num_notifications_;
183+
if (num_notifications_ == n_owners)
184+
{
185+
cv_.notify_all();
186+
}
187+
188+
cv_.wait(lock, [this]() -> bool
189+
{
190+
return num_notifications_ >= n_owners;
191+
});
192+
}
193+
194+
void wait_for_all_notifications()
195+
{
196+
std::unique_lock<std::mutex> lock(mutex_);
197+
cv_.wait(lock, [this]() -> bool
198+
{
199+
return num_notifications_ == n_owners;
200+
});
201+
}
202+
203+
private:
204+
205+
std::mutex mutex_;
206+
std::condition_variable cv_;
207+
std::size_t num_notifications_;
208+
};
209+
210+
WaitForAllOwners sync_point;
211+
156212
// Spawn some routines
157213
for (std::size_t i = 0; i < n_owners; ++i)
158214
{
159-
owners_[i].spawn_routine();
215+
owners_[i].spawn_routine(&sync_point);
160216
}
161217

162-
// Ensure owners' routines have started
163-
std::this_thread::sleep_for(std::chrono::milliseconds(20));
218+
// Wait for all routines to be started
219+
sync_point.wait_for_all_notifications();
164220

165221
auto t0 = std::chrono::steady_clock::now();
166222
entity_.destroy();
167223
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - t0).count();
168224

169225
std::cout << "Elapsed time: " << elapsed << " ms" << std::endl;
170-
ASSERT_GT(elapsed, 50); // destroy should have taken at least 50 ms. Being strict it should be 80, but we allow some margin
226+
ASSERT_GT(elapsed, 50); // destroy should have taken at least 50 ms. Being strict it should be 100, but we allow some margin
171227
ASSERT_EQ(entity_.n_times_data_processed, 5);
172228
}
173229

0 commit comments

Comments
 (0)