spring-framework
spring-framework copied to clipboard
Polish async test by removing sleep in DefaultAsyncServerResponseTests
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)insideCompletableFuture.supplyAsync - Replaced it with a non-blocking test pattern:
- Created an incomplete
CompletableFuture - Completed it asynchronously using
new Thread(() -> future.complete(...))
- Created an incomplete
- 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.