|
| 1 | +package com.webfleet.assertj; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | +import java.util.function.Consumer; |
| 6 | + |
| 7 | +import org.assertj.core.api.SoftAssertions; |
| 8 | + |
| 9 | +import lombok.NonNull; |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * Asynchronous assertion. |
| 14 | + */ |
| 15 | +public interface AsyncAssert |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Awaits until all assertions are passed or timeout is exceeded. |
| 19 | + * Assertions are configured in lambda consumer of {@link SoftAssertions} object on each check. |
| 20 | + * The checks are executed periodically with check interval delay configured with {@link AsyncAssert#withWaitInterval} method. |
| 21 | + * After exceeding timeout {@link AssertionError} will be thrown with failures from last assertion check. |
| 22 | + * |
| 23 | + * <pre><code class='java'> |
| 24 | + * awaitAtMostOneSecond().untilAssertions(async -> { |
| 25 | + * async.assertThat(getValue()).isEqualTo(expected); |
| 26 | + * async.assertThat(isDone()).isTrue(); |
| 27 | + * }); |
| 28 | + * </code></pre> |
| 29 | + * |
| 30 | + * @param assertionsConfiguerer lambda consumer configuring {@link SoftAssertions} object |
| 31 | + */ |
| 32 | + void untilAssertions(Consumer<SoftAssertions> assertionsConfiguerer); |
| 33 | + |
| 34 | + /** |
| 35 | + * Configures assertion to use give mutex object for check interval wait logic. |
| 36 | + * |
| 37 | + * In multi-threaded applications, the mutex object can be used to notify the other thread about state change. |
| 38 | + * For asynchronous assertion, the mutex object can be used to reduce the wait time between checks with {@link Object#notifyAll()} call. |
| 39 | + * |
| 40 | + * @param waitMutex mutex object |
| 41 | + * @return new {@link AsyncAssert} using given wait mutex |
| 42 | + */ |
| 43 | + AsyncAssert usingWaitMutex(Object waitMutex); |
| 44 | + |
| 45 | + /** |
| 46 | + * Configures the interval to be waited between assertions checks. |
| 47 | + * The interval must be greater than zero and lower than timeout. |
| 48 | + * |
| 49 | + * @param checkInterval check interval |
| 50 | + * @return new {@link AsyncAssert} with set check interval |
| 51 | + */ |
| 52 | + AsyncAssert withCheckInterval(Duration checkInterval); |
| 53 | + |
| 54 | + /** |
| 55 | + * Configures the interval to be waited between assertions checks. |
| 56 | + * The interval must be greater than zero and lower than timeout. |
| 57 | + * |
| 58 | + * @param checkInterval check interval |
| 59 | + * @param unit the time unit of the check interval |
| 60 | + * @return new {@link AsyncAssert} with set check interval |
| 61 | + */ |
| 62 | + default AsyncAssert withWaitInterval(final long checkInterval, @NonNull final TimeUnit unit) |
| 63 | + { |
| 64 | + return withCheckInterval(Duration.ofMillis(unit.toMillis(checkInterval))); |
| 65 | + } |
| 66 | +} |
0 commit comments