Skip to content

Commit 76dc535

Browse files
author
Mauro Passerino
committed
Fix spin_once test
Signed-off-by: Mauro Passerino <[email protected]>
1 parent 9b13b77 commit 76dc535

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

rclcpp/test/rclcpp/executors/test_events_executor.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,58 @@ TEST_F(TestEventsExecutor, spin_once_max_duration)
118118
{
119119
auto node = std::make_shared<rclcpp::Node>("node");
120120

121+
EventsExecutor executor;
122+
executor.add_node(node);
123+
124+
// Consume previous events so we have a fresh start
125+
executor.spin_all(1s);
126+
121127
size_t t_runs = 0;
122128
auto t = node->create_wall_timer(
123129
10s,
124130
[&]() {
125131
t_runs++;
126132
});
127133

128-
EventsExecutor executor;
129-
executor.add_node(node);
134+
// This first spin_once takes care of the waitable event
135+
// generated by the addition of the timer to the node
136+
executor.spin_once(1s);
137+
EXPECT_EQ(0u, t_runs);
130138

131139
auto start = std::chrono::steady_clock::now();
140+
141+
// This second spin_once should take care of the timer,
132142
executor.spin_once(10ms);
133143

144+
// but doesn't spin the time enough to call the timer callback.
134145
EXPECT_EQ(0u, t_runs);
135146
EXPECT_TRUE(std::chrono::steady_clock::now() - start < 200ms);
136147
}
137148

138149
{
139150
auto node = std::make_shared<rclcpp::Node>("node");
140151

152+
EventsExecutor executor;
153+
executor.add_node(node);
154+
155+
// Consume previous events so we have a fresh start
156+
executor.spin_all(1s);
157+
141158
size_t t_runs = 0;
142159
auto t = node->create_wall_timer(
143160
10ms,
144161
[&]() {
145162
t_runs++;
146163
});
147164

148-
EventsExecutor executor;
149-
executor.add_node(node);
165+
// This first spin_once takes care of the waitable event
166+
// generated by the addition of the timer to the node
167+
executor.spin_once(1s);
168+
EXPECT_EQ(0u, t_runs);
150169

151170
auto start = std::chrono::steady_clock::now();
171+
172+
// This second spin_once should take care of the timer
152173
executor.spin_once(10s);
153174

154175
EXPECT_EQ(1u, t_runs);
@@ -277,24 +298,28 @@ TEST_F(TestEventsExecutor, cancel_while_timers_running)
277298
{
278299
auto node = std::make_shared<rclcpp::Node>("node");
279300

301+
EventsExecutor executor;
302+
executor.add_node(node);
303+
304+
// Take care of previous events for a fresh start
305+
executor.spin_all(1s);
306+
280307
size_t t1_runs = 0;
281308
auto t1 = node->create_wall_timer(
282309
1ms,
283310
[&]() {
284311
t1_runs++;
285-
std::this_thread::sleep_for(25ms);
312+
std::this_thread::sleep_for(50ms);
286313
});
287314

288315
size_t t2_runs = 0;
289316
auto t2 = node->create_wall_timer(
290317
1ms,
291318
[&]() {
292319
t2_runs++;
293-
std::this_thread::sleep_for(25ms);
320+
std::this_thread::sleep_for(50ms);
294321
});
295322

296-
EventsExecutor executor;
297-
executor.add_node(node);
298323

299324
std::thread spinner([&executor, this]() {executor.spin();});
300325

0 commit comments

Comments
 (0)