@@ -105,6 +105,9 @@ TEST_F(TestMultiThreadedExecutor, timer_over_take) {
105105 Test that no tasks are starved
106106 */
107107<<<<<<< HEAD
108+ <<<<<<< HEAD
109+ =======
110+ >>>>>>> 104b5d5b (Updated test for multi-threaded executor starvation)
108111/* *
109112 * @brief Test case for checking starvation in MultiThreadedExecutor.
110113 *
@@ -118,6 +121,7 @@ TEST_F(TestMultiThreadedExecutor, timer_over_take) {
118121 * starve any of the timers and that both timers are executed in a balanced
119122 * manner.
120123 */
124+ <<<<<<< HEAD
121125TEST_F (TestMultiThreadedExecutor, starvation) {
122126 rclcpp::executors::MultiThreadedExecutor executor (rclcpp::ExecutorOptions (),
123127 2u );
@@ -170,62 +174,56 @@ TEST_F(TestMultiThreadedExecutor, starvation) {
170174 executor.spin ();
171175}
172176=======
177+ =======
178+ >>>>>>> 104b5d5b (Updated test for multi-threaded executor starvation)
173179TEST_F (TestMultiThreadedExecutor, starvation) {
174180 rclcpp::executors::MultiThreadedExecutor executor (rclcpp::ExecutorOptions (),
175181 2u );
176-
182+ // Create a node for the test
177183 std::shared_ptr<rclcpp::Node> node =
178184 std::make_shared<rclcpp::Node>(" test_multi_threaded_executor_starvation" );
179185
186+ // Atomic counters for the timers
180187 std::atomic_int timer_one_count{0 };
181188 std::atomic_int timer_two_count{0 };
182189
183- rclcpp::TimerBase::SharedPtr timer_one;
184- rclcpp::TimerBase::SharedPtr timer_two;
185-
186- auto timer_one_callback = [&timer_one_count, &timer_two_count]() {
187- std::cout << " Timer one callback executed. Count: "
188- << timer_one_count.load () << std::endl;
189-
190- auto start_time = std::chrono::steady_clock::now ();
191- while (std::chrono::steady_clock::now () - start_time < 100ms) {
192- }
193-
194- timer_one_count++;
195-
196- auto diff = std::abs (timer_one_count - timer_two_count);
197-
198- std::cout << " Difference in counts: " << diff << std::endl;
199-
200- if (diff > 1 ) {
201- rclcpp::shutdown ();
202- ASSERT_LE (diff, 1 );
203- }
204- };
205-
206- auto timer_two_callback = [&timer_one_count, &timer_two_count]() {
207- std::cout << " Timer two callback executed. Count: "
208- << timer_two_count.load () << std::endl;
190+ // 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;
209195
196+ // Simulate work by busy-waiting for 100ms
210197 auto start_time = std::chrono::steady_clock::now ();
211198 while (std::chrono::steady_clock::now () - start_time < 100ms) {
212199 }
213200
214- timer_two_count++;
201+ // Increment the counter for the first timer
202+ count_one++;
215203
216- auto diff = std::abs (timer_one_count - timer_two_count);
204+ // Calculate the difference between the two counters
205+ auto diff = std::abs (count_one - count_two);
217206
218207 std::cout << " Difference in counts: " << diff << std::endl;
219208
209+ // If the difference exceeds 1, shut down and assert failure
220210 if (diff > 1 ) {
221211 rclcpp::shutdown ();
222212 ASSERT_LE (diff, 1 );
223213 }
224214 };
225215
226- timer_one = node->create_wall_timer (0ms, timer_one_callback);
227- timer_two = node->create_wall_timer (0ms, timer_two_callback);
216+ // Create the timers with 0ms period
217+ auto timer_one = node->create_wall_timer (
218+ 0ms, [timer_one_callback, &timer_one_count, &timer_two_count]() {
219+ timer_one_callback (timer_one_count, timer_two_count);
220+ });
221+ auto timer_two = node->create_wall_timer (
222+ 0ms, [timer_one_callback, &timer_two_count, &timer_one_count]() {
223+ timer_one_callback (timer_two_count, timer_one_count);
224+ });
228225
226+ // Add the node to the executor and spin
229227 executor.add_node (node);
230228 executor.spin ();
231229}
0 commit comments