spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Polish async test by removing sleep in DefaultAsyncServerResponseTests

Open hydrationn opened this issue 1 day ago • 0 comments

This PR removes Thread.sleep from async tests in DefaultAsyncServerResponseTests and replaces it with a non-blocking CompletableFuture pattern. This makes the test more deterministic and avoids timing-based flakiness.

🔍 What was changed

  • Removed Thread.sleep(500) inside CompletableFuture.supplyAsync
  • Replaced it with a non-blocking test pattern:
    • Created an incomplete CompletableFuture
    • Completed it asynchronously using new Thread(() -> future.complete(...))
  • This makes the test more deterministic and avoids relying on timing-based behavior

💡 Why this change

Using Thread.sleep in tests is generally discouraged because:

  • It increases test execution time
  • It may lead to unstable test results depending on system timing
  • It does not reflect real asynchronous behavior

The updated approach ensures:

  • Faster test execution
  • More reliable async behavior
  • Clearer test intent

📌 Notes

No functional changes were made to production code.
This update improves the internal test quality and stability.

hydrationn avatar Dec 10 '25 17:12 hydrationn