Skip to content

Commit 293bbfd

Browse files
committed
Refactor multi-threaded executor tests for improved readability and consistency
1 parent 26f9fa5 commit 293bbfd

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

rclcpp/test/rclcpp/executors/test_multi_threaded_executor.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
using namespace std::chrono_literals;
2828

2929
class TestMultiThreadedExecutor : public ::testing::Test {
30-
protected:
31-
static void SetUpTestCase() { rclcpp::init(0, nullptr); }
30+
protected:
31+
static void SetUpTestCase() {rclcpp::init(0, nullptr);}
3232

33-
static void TearDownTestCase() { rclcpp::shutdown(); }
33+
static void TearDownTestCase() {rclcpp::shutdown();}
3434
};
3535

3636
constexpr std::chrono::milliseconds PERIOD_MS = 1000ms;
@@ -55,7 +55,7 @@ TEST_F(TestMultiThreadedExecutor, timer_over_take) {
5555
bool yield_before_execute = true;
5656

5757
rclcpp::executors::MultiThreadedExecutor executor(rclcpp::ExecutorOptions(),
58-
2u, yield_before_execute);
58+
2u, yield_before_execute);
5959

6060
ASSERT_GT(executor.get_number_of_threads(), 1u);
6161

@@ -71,30 +71,30 @@ TEST_F(TestMultiThreadedExecutor, timer_over_take) {
7171
std::atomic_int timer_count{0};
7272

7373
auto timer_callback = [&timer_count, &executor, &system_clock, &last_mutex,
74-
&last]() {
74+
&last]() {
7575
// While this tolerance is a little wide, if the bug occurs, the next step
7676
// will happen almost instantly. The purpose of this test is not to measure
7777
// the jitter in timers, just assert that a reasonable amount of time has
7878
// passed.
79-
rclcpp::Time now = system_clock.now();
80-
timer_count++;
79+
rclcpp::Time now = system_clock.now();
80+
timer_count++;
8181

82-
if (timer_count > 5) {
83-
executor.cancel();
84-
}
82+
if (timer_count > 5) {
83+
executor.cancel();
84+
}
8585

86-
{
87-
std::lock_guard<std::mutex> lock(last_mutex);
88-
double diff =
86+
{
87+
std::lock_guard<std::mutex> lock(last_mutex);
88+
double diff =
8989
static_cast<double>(std::abs((now - last).nanoseconds())) / 1.0e9;
90-
last = now;
90+
last = now;
9191

92-
if (diff < PERIOD - TOLERANCE) {
93-
executor.cancel();
94-
ASSERT_GT(diff, PERIOD - TOLERANCE);
92+
if (diff < PERIOD - TOLERANCE) {
93+
executor.cancel();
94+
ASSERT_GT(diff, PERIOD - TOLERANCE);
95+
}
9596
}
96-
}
97-
};
97+
};
9898

9999
auto timer = node->create_wall_timer(PERIOD_MS, timer_callback, cbg);
100100
executor.add_node(node);
@@ -178,53 +178,56 @@ TEST_F(TestMultiThreadedExecutor, starvation) {
178178
>>>>>>> 104b5d5b (Updated test for multi-threaded executor starvation)
179179
TEST_F(TestMultiThreadedExecutor, starvation) {
180180
rclcpp::executors::MultiThreadedExecutor executor(rclcpp::ExecutorOptions(),
181-
2u);
181+
2u);
182182
// Create a node for the test
183183
std::shared_ptr<rclcpp::Node> node =
184-
std::make_shared<rclcpp::Node>("test_multi_threaded_executor_starvation");
184+
std::make_shared<rclcpp::Node>("test_multi_threaded_executor_starvation");
185185

186186
// Atomic counters for the timers
187187
std::atomic_int timer_one_count{0};
188188
std::atomic_int timer_two_count{0};
189189

190190
// Callback for the timers
191-
auto timer_one_callback = [](std::atomic_int &count_one,
192-
std::atomic_int &count_two) -> void {
193-
std::cout << "Timer one callback executed. Count: " << count_one.load()
194-
<< std::endl;
191+
auto timer_one_callback = [](std::atomic_int & count_one,
192+
std::atomic_int & count_two) -> void {
193+
std::cout << "Timer one callback executed. Count: " << count_one.load()
194+
<< std::endl;
195195

196196
// Simulate work by busy-waiting for 100ms
197-
auto start_time = std::chrono::steady_clock::now();
198-
while (std::chrono::steady_clock::now() - start_time < 100ms) {
199-
}
197+
auto start_time = std::chrono::steady_clock::now();
198+
while (std::chrono::steady_clock::now() - start_time < 100ms) {
199+
}
200200

201201
// Increment the counter for the first timer
202-
count_one++;
202+
count_one++;
203203

204204
// Calculate the difference between the two counters
205-
auto diff = std::abs(count_one - count_two);
205+
auto diff = std::abs(count_one - count_two);
206206

207-
std::cout << "Difference in counts: " << diff << std::endl;
207+
std::cout << "Difference in counts: " << diff << std::endl;
208208

209209
// If the difference exceeds 1, shut down and assert failure
210-
if (diff > 1) {
211-
rclcpp::shutdown();
212-
ASSERT_LE(diff, 1);
213-
}
214-
};
210+
if (diff > 1) {
211+
rclcpp::shutdown();
212+
ASSERT_LE(diff, 1);
213+
}
214+
};
215215

216216
// Create the timers with 0ms period
217217
auto timer_one = node->create_wall_timer(
218218
0ms, [timer_one_callback, &timer_one_count, &timer_two_count]() {
219-
timer_one_callback(timer_one_count, timer_two_count);
219+
timer_one_callback(timer_one_count, timer_two_count);
220220
});
221221
auto timer_two = node->create_wall_timer(
222222
0ms, [timer_one_callback, &timer_two_count, &timer_one_count]() {
223-
timer_one_callback(timer_two_count, timer_one_count);
223+
timer_one_callback(timer_two_count, timer_one_count);
224224
});
225225

226226
// Add the node to the executor and spin
227227
executor.add_node(node);
228228
executor.spin();
229229
}
230+
<<<<<<< HEAD
230231
>>>>>>> b8596345 (Added starvation test)
232+
=======
233+
>>>>>>> 15c6c501 (Refactor multi-threaded executor tests for improved readability and consistency)

0 commit comments

Comments
 (0)