[Feature](test)Using Awaitility for Asynchronous Testing
Search before asking
- [X] I had searched in the issues and found no similar issues.
Description
Currently our test codes include a lot of thread.sleep test codes, but this is not a recommended test method.
On one hand, Thread.sleep can lead to unstable test cases. On the other hand, it can also cause test cases to run longer than necessary.
https://github.com/awaitility/awaitility
Thread.sleep vs Awaitility
Using Awaitility instead of Thread.sleep for asynchronous testing in Java offers several advantages:
Readability and Intent:
Awaitility: Using Awaitility makes the test code more readable and expressive. It clearly communicates the intention to wait for a certain condition to be met.
Awaitility.await().until(() -> someConditionIsMet());
Thread.sleep: Using Thread.sleep adds an arbitrary delay without indicating why the delay is necessary, making the code less readable and harder to understand.
Thread.sleep(1000);
Reliability:
Awaitility: Waits until a specific condition is met, which makes the tests more reliable and less prone to intermittent failures. It ensures that the test only proceeds when the expected condition is true. Thread.sleep: Introduces a fixed delay, which can lead to flaky tests. If the condition is met sooner than the sleep time, the test unnecessarily waits. Conversely, if the condition is met after the sleep time, the test will fail. Timeout Management: Awaitility: Allows for configuring timeouts and poll intervals, giving fine-grained control over how long to wait and how frequently to check the condition.
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> someConditionIsMet());
Thread.sleep: Error Messages: Awaitility: Provides clear error messages when a condition is not met within the specified timeout, making it easier to diagnose issues. Thread.sleep: If the condition is not met, the test may fail without a clear explanation, making debugging more challenging.
Best Practices:
Awaitility: Encourages best practices for writing asynchronous tests, such as waiting for conditions rather than introducing arbitrary delays. Thread.sleep: Using Thread.sleep is generally considered a bad practice in tests because it can lead to non-deterministic behavior and inefficiencies. In summary, Awaitility improves test readability, reliability, and maintainability by allowing precise control over waiting for asynchronous conditions, making it a better choice than using Thread.sleep.
Related issues
No response
Are you willing to submit PR?
- [ ] Yes I am willing to submit a PR!
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
I want to try this issue, can you assign it to me?
I want to try this issue, can you assign it to me?↳
Sure! Feel free to contribute. Since this work involves a lot, you can break it down into small PRs and submit them. Of course, it’s best to comment below on which test cases you plan to refactor.
@CalvinKirs Hi , i have raise few MR for this task. pls help to review. thanks.