@@ -1695,26 +1695,34 @@ omitted. Specifying no unit is equivalent to using seconds.
1695
1695
| `42 d` | `@Timeout(value = 42, unit = DAYS)`
1696
1696
|===
1697
1697
1698
- ==== Use @Timeout for polling tests
1699
1698
1700
- It is common to write tests waiting for some updates.
1701
- In some cases you can rewrite the logic to use a `CountDownLatch` or any other synchronization
1702
- mechanism but sometimes it is not possible - generally when depending on an external system.
1703
- These tests require some timeout to ensure they don't hang the test suite forever and
1704
- the test fails if the waiting condition "never" happens.
1705
- With `@Timeout` it becomes very easy to write such tests since the test itself just require
1706
- to become an infinite loop:
1699
+ [[writing-tests-declarative-timeouts-polling]]
1700
+ ==== Using @Timeout for Polling Tests
1701
+
1702
+ When dealing with asynchronous code, it is common to write tests that poll while waiting
1703
+ for something to happen before performing any assertions. In some cases you can rewrite
1704
+ the logic to use a `CountDownLatch` or another synchronization mechanism, but sometimes
1705
+ that is not possible — for example, if the subject under test sends a message to a channel
1706
+ in an external message broker and assertions cannot be performed until the message has
1707
+ been successfully sent through the channel. Asynchronous tests like these require some
1708
+ form of timeout to ensure they don't hang the test suite by executing indefinitely, as
1709
+ would be the case if an asynchronous message never gets successfully delivered.
1710
+
1711
+ By configuring a timeout for an asynchronous test that polls, you can ensure that the test
1712
+ does not execute indefinitely. The following example demonstrates how to achieve this with
1713
+ JUnit Jupiter's `@Timeout` annotation. This technique can be used to implement "poll
1714
+ until" logic very easily.
1707
1715
1708
1716
[source,java]
1709
1717
----
1710
- include::{testDir}/example/PollingTimeout .java[tags=user_guide,indent=0]
1718
+ include::{testDir}/example/PollingTimeoutDemo .java[tags=user_guide,indent=0]
1711
1719
----
1712
1720
1713
- Such a simple usage enables to implement "test when" or "wait until" logic very easily.
1714
-
1715
- Alternatively, you can use a library handling for you the awaiting like the well-known
1721
+ NOTE: If you need more control over polling intervals and greater flexibility with
1722
+ asynchronous tests, consider using a dedicated library such as
1716
1723
link:https://github.com/awaitility/awaitility[Awaitility].
1717
1724
1725
+
1718
1726
[[writing-tests-parallel-execution]]
1719
1727
=== Parallel Execution
1720
1728
0 commit comments