|
30 | 30 |
|
31 | 31 | #include <chrono> |
32 | 32 | #include <condition_variable> |
| 33 | +#include <functional> |
33 | 34 | #include <memory> |
34 | 35 | #include <mutex> |
35 | 36 | #include <string> |
@@ -120,6 +121,41 @@ class TrajectoryStreamingTest : public ::testing::Test |
120 | 121 | } |
121 | 122 | return control::TrajectoryResult::TRAJECTORY_RESULT_UNKNOWN; |
122 | 123 | } |
| 124 | + |
| 125 | + // Variant that fires a single user-supplied action exactly once after |
| 126 | + // `inject_at` has elapsed since the call started. The action takes the |
| 127 | + // place of one NOOP-pump iteration. Used to inject a stray trajectory |
| 128 | + // control message mid-flight without spinning up a second thread. |
| 129 | + control::TrajectoryResult waitForTrajectoryResultPumpingNoopsWithInjection( |
| 130 | + std::chrono::milliseconds total_timeout, std::chrono::milliseconds inject_at, |
| 131 | + std::function<void()> inject_action) |
| 132 | + { |
| 133 | + const auto start = std::chrono::steady_clock::now(); |
| 134 | + const auto deadline = start + total_timeout; |
| 135 | + const auto inject_deadline = start + inject_at; |
| 136 | + bool injected = false; |
| 137 | + while (std::chrono::steady_clock::now() < deadline) |
| 138 | + { |
| 139 | + std::unique_lock<std::mutex> lk(g_trajectory_result_mutex); |
| 140 | + if (g_trajectory_result_cv.wait_for(lk, std::chrono::milliseconds(50), |
| 141 | + [] { return g_trajectory_result_received; })) |
| 142 | + { |
| 143 | + return g_trajectory_result; |
| 144 | + } |
| 145 | + lk.unlock(); |
| 146 | + if (!injected && std::chrono::steady_clock::now() >= inject_deadline) |
| 147 | + { |
| 148 | + inject_action(); |
| 149 | + injected = true; |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 154 | + control::TrajectoryControlMessage::TRAJECTORY_NOOP); |
| 155 | + } |
| 156 | + } |
| 157 | + return control::TrajectoryResult::TRAJECTORY_RESULT_UNKNOWN; |
| 158 | + } |
123 | 159 | }; |
124 | 160 |
|
125 | 161 | // Clean end-to-end stream: STREAM_START, N spline points at a held pose, |
@@ -197,6 +233,158 @@ TEST_F(TrajectoryStreamingTest, stream_cancel_yields_canceled) |
197 | 233 | waitForTrajectoryResultPumpingNoops(std::chrono::seconds(5))); |
198 | 234 | } |
199 | 235 |
|
| 236 | +// Regression test for PR #528 cursor-bot review comment 1a. |
| 237 | +// A stray TRAJECTORY_STREAM_END dispatched during a legacy finite |
| 238 | +// TRAJECTORY_START trajectory must not corrupt trajectory_points_left. |
| 239 | +// Without the streaming-guard fix, the dispatcher's STREAM_END math |
| 240 | +// (which assumes trajectory_points_left is a sentinel-based credit |
| 241 | +// budget) computes a huge negative value, the trajectoryThread's |
| 242 | +// "trajectory_points_left > 0" predicate fails on the next iteration, |
| 243 | +// the loop exits while trajectory_result is still SUCCESS, and the |
| 244 | +// callback fires SUCCESS even though motion was truncated. |
| 245 | +TEST_F(TrajectoryStreamingTest, stray_stream_end_during_finite_trajectory_does_not_truncate) |
| 246 | +{ |
| 247 | + const vector6d_t pose_a = { 0.0, -1.57, 0.0, -1.57, 0.0, 0.0 }; |
| 248 | + const vector6d_t pose_b = { 0.0, -1.40, 0.0, -1.57, 0.0, 0.0 }; |
| 249 | + const vector6d_t pose_c = { 0.0, -1.20, 0.0, -1.57, 0.0, 0.0 }; |
| 250 | + const vector6d_t zero = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
| 251 | + |
| 252 | + // Pre-position |
| 253 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 254 | + control::TrajectoryControlMessage::TRAJECTORY_START, 1)); |
| 255 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(pose_a, zero, zero, 2.0f)); |
| 256 | + ASSERT_EQ(control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS, |
| 257 | + waitForTrajectoryResultPumpingNoops(std::chrono::seconds(5))); |
| 258 | + resetTrajectoryResultState(); |
| 259 | + |
| 260 | + // Send a 3-point finite trajectory, each segment ~1 second. |
| 261 | + const float k_segment_time = 1.0f; |
| 262 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 263 | + control::TrajectoryControlMessage::TRAJECTORY_START, 3)); |
| 264 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(pose_b, zero, zero, k_segment_time)); |
| 265 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(pose_c, zero, zero, k_segment_time)); |
| 266 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(pose_a, zero, zero, k_segment_time)); |
| 267 | + |
| 268 | + const auto motion_start = std::chrono::steady_clock::now(); |
| 269 | + const auto result = waitForTrajectoryResultPumpingNoopsWithInjection( |
| 270 | + std::chrono::seconds(6), std::chrono::milliseconds(500), [] { |
| 271 | + g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 272 | + control::TrajectoryControlMessage::TRAJECTORY_STREAM_END, 0); |
| 273 | + }); |
| 274 | + const auto elapsed = std::chrono::steady_clock::now() - motion_start; |
| 275 | + |
| 276 | + EXPECT_EQ(control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS, result); |
| 277 | + // Three 1-second segments expect ~3 s of motion. Without the fix, the |
| 278 | + // stray STREAM_END truncates the loop after segment 1, callback fires |
| 279 | + // in ~1 s. |
| 280 | + EXPECT_GE(elapsed, std::chrono::milliseconds(2500)) |
| 281 | + << "Expected ~3 s of motion; stray STREAM_END appears to have truncated the trajectory."; |
| 282 | +} |
| 283 | + |
| 284 | +// Regression test for PR #528 cursor-bot review comment 1c. |
| 285 | +// A second TRAJECTORY_STREAM_END dispatched during the drain phase |
| 286 | +// re-applies the sentinel-based math to a non-sentinel |
| 287 | +// trajectory_points_left value, producing a huge negative value and |
| 288 | +// truncating the drain while reporting SUCCESS. Without the streaming- |
| 289 | +// guard fix the second STREAM_END is destructive; with the fix it is |
| 290 | +// a no-op because trajectory_streaming was cleared by the first. |
| 291 | +TEST_F(TrajectoryStreamingTest, double_stream_end_does_not_truncate) |
| 292 | +{ |
| 293 | + const vector6d_t held_pose = { 0.0, -1.57, 0.0, -1.57, 0.0, 0.0 }; |
| 294 | + const vector6d_t zero = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
| 295 | + |
| 296 | + // Pre-position |
| 297 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 298 | + control::TrajectoryControlMessage::TRAJECTORY_START, 1)); |
| 299 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(held_pose, zero, zero, 2.0f)); |
| 300 | + ASSERT_EQ(control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS, |
| 301 | + waitForTrajectoryResultPumpingNoops(std::chrono::seconds(5))); |
| 302 | + resetTrajectoryResultState(); |
| 303 | + |
| 304 | + // Stream 20 points with tmptime=0.1 s each. Nominal consumer-side |
| 305 | + // execution is ~2 s. |
| 306 | + const int k_num_points = 20; |
| 307 | + const float k_step_time = 0.1f; |
| 308 | + |
| 309 | + const auto motion_start = std::chrono::steady_clock::now(); |
| 310 | + |
| 311 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 312 | + control::TrajectoryControlMessage::TRAJECTORY_STREAM_START)); |
| 313 | + for (int i = 0; i < k_num_points; ++i) |
| 314 | + { |
| 315 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(held_pose, zero, zero, k_step_time)); |
| 316 | + } |
| 317 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 318 | + control::TrajectoryControlMessage::TRAJECTORY_STREAM_END, k_num_points)); |
| 319 | + // Second STREAM_END immediately after the first. Without the guard fix |
| 320 | + // this re-applies the sentinel-based math to the already-recomputed |
| 321 | + // counter and truncates the drain. |
| 322 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 323 | + control::TrajectoryControlMessage::TRAJECTORY_STREAM_END, k_num_points)); |
| 324 | + |
| 325 | + const auto result = waitForTrajectoryResultPumpingNoops(std::chrono::seconds(5)); |
| 326 | + const auto elapsed = std::chrono::steady_clock::now() - motion_start; |
| 327 | + |
| 328 | + EXPECT_EQ(control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS, result); |
| 329 | + EXPECT_GE(elapsed, std::chrono::milliseconds(1500)) |
| 330 | + << "Expected ~2 s of consumer-side execution; double STREAM_END appears to have truncated the drain."; |
| 331 | +} |
| 332 | + |
| 333 | +// Regression test for PR #528 cursor-bot review comment 2. |
| 334 | +// A post-STREAM_END timeout-on-read with trajectory_points_left still |
| 335 | +// positive must be reported as TRAJECTORY_RESULT_FAILURE, not silently |
| 336 | +// swallowed as SUCCESS. The current clean-end shortcut fires whenever |
| 337 | +// trajectory_streaming was just cleared, regardless of whether the |
| 338 | +// consumer still expects more points. The fix narrows the shortcut to |
| 339 | +// the only legitimate case (count_after_decrement < 0), which can only |
| 340 | +// arise via a race where STREAM_END landed during the read block after |
| 341 | +// the consumer had already drained the buffer. |
| 342 | +TEST_F(TrajectoryStreamingTest, stream_end_with_overcount_yields_failure) |
| 343 | +{ |
| 344 | + const vector6d_t held_pose = { 0.0, -1.57, 0.0, -1.57, 0.0, 0.0 }; |
| 345 | + const vector6d_t zero = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
| 346 | + |
| 347 | + // Pre-position |
| 348 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 349 | + control::TrajectoryControlMessage::TRAJECTORY_START, 1)); |
| 350 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(held_pose, zero, zero, 2.0f)); |
| 351 | + ASSERT_EQ(control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS, |
| 352 | + waitForTrajectoryResultPumpingNoops(std::chrono::seconds(5))); |
| 353 | + resetTrajectoryResultState(); |
| 354 | + |
| 355 | + // Send 50 real points but tell STREAM_END the producer wrote 100. |
| 356 | + const int k_actually_sent = 50; |
| 357 | + const int k_announced = 100; |
| 358 | + const float k_step_time = 0.01f; |
| 359 | + |
| 360 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 361 | + control::TrajectoryControlMessage::TRAJECTORY_STREAM_START)); |
| 362 | + for (int i = 0; i < k_actually_sent; ++i) |
| 363 | + { |
| 364 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectorySplinePoint(held_pose, zero, zero, k_step_time)); |
| 365 | + } |
| 366 | + // The STREAM_END must arrive after the trajectoryThread has executed |
| 367 | + // its `was_streaming_at_start = trajectory_streaming` assignment with |
| 368 | + // the streaming flag still True. Otherwise a race - URScript |
| 369 | + // scheduling the dispatcher's next iteration before the new thread's |
| 370 | + // first statement runs - causes was_streaming_at_start to be captured |
| 371 | + // as False, which sends any underrun straight down the legacy FAILURE |
| 372 | + // arm regardless of the bug under test. Pump NOOPs for ~250 ms to |
| 373 | + // give the thread plenty of time to start and consume some points |
| 374 | + // before STREAM_END is dispatched. |
| 375 | + for (int i = 0; i < 5; ++i) |
| 376 | + { |
| 377 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 378 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 379 | + control::TrajectoryControlMessage::TRAJECTORY_NOOP)); |
| 380 | + } |
| 381 | + ASSERT_TRUE(g_my_robot->getUrDriver()->writeTrajectoryControlMessage( |
| 382 | + control::TrajectoryControlMessage::TRAJECTORY_STREAM_END, k_announced)); |
| 383 | + |
| 384 | + EXPECT_EQ(control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE, |
| 385 | + waitForTrajectoryResultPumpingNoops(std::chrono::seconds(5))); |
| 386 | +} |
| 387 | + |
200 | 388 | int main(int argc, char* argv[]) |
201 | 389 | { |
202 | 390 | ::testing::InitGoogleTest(&argc, argv); |
|
0 commit comments