Skip to content

Commit 4f2da09

Browse files
committed
Polish contribution
See #1926
1 parent e6006fe commit 4f2da09

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

+20-12
Original file line numberDiff line numberDiff line change
@@ -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
17161723
link:https://github.com/awaitility/awaitility[Awaitility].
17171724

1725+
17181726
[[writing-tests-parallel-execution]]
17191727
=== Parallel Execution
17201728

documentation/src/test/java/example/PollingTimeout.java renamed to documentation/src/test/java/example/PollingTimeoutDemo.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010

1111
package example;
1212

13+
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.api.Timeout;
1415

15-
class PollingTimeout {
16+
class PollingTimeoutDemo {
17+
1618
// tag::user_guide[]
17-
@Timeout(5) // 5s
18-
void waitUntil() throws InterruptedException {
19-
while (!isConditionTrue()) {
20-
Thread.sleep(250); // use some adapted retry duration
19+
@Test
20+
@Timeout(5) // Poll at most 5 seconds
21+
void pollUntil() throws InterruptedException {
22+
while (asynchronousResultNotAvailable()) {
23+
Thread.sleep(250); // custom poll interval
2124
}
22-
// if needed asserts on the result of the awaited condition
25+
// Obtain the asynchronous result and perform assertions
2326
}
2427
// end::user_guide[]
2528

26-
private boolean isConditionTrue() {
27-
return true;
29+
private boolean asynchronousResultNotAvailable() {
30+
return false;
2831
}
32+
2933
}

0 commit comments

Comments
 (0)