@@ -1695,26 +1695,34 @@ omitted. Specifying no unit is equivalent to using seconds.
16951695| `42 d` | `@Timeout(value = 42, unit = DAYS)`
16961696|===
16971697
1698- ==== Use @Timeout for polling tests
16991698
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.
17071715
17081716[source,java]
17091717----
1710- include::{testDir}/example/PollingTimeout .java[tags=user_guide,indent=0]
1718+ include::{testDir}/example/PollingTimeoutDemo .java[tags=user_guide,indent=0]
17111719----
17121720
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
17161723link:https://github.com/awaitility/awaitility[Awaitility].
17171724
1725+
17181726[[writing-tests-parallel-execution]]
17191727=== Parallel Execution
17201728
0 commit comments