Package org.awaitility.core
Class ConditionFactory
- java.lang.Object
-
- org.awaitility.core.ConditionFactory
-
public class ConditionFactory extends java.lang.ObjectA factory for creatingConditionobjects. It's not recommended instantiating this class directly.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringaliasThe alias.private booleancatchUncaughtExceptionsThe catch uncaught exceptions.private ConditionEvaluationListenerconditionEvaluationListenerThe condition evaluation listenerprivate ExceptionIgnorerexceptionsIgnorerThe ignore exceptions.private ExecutorLifecycleexecutorLifecycleThe executor lifecycleprivate FailFastConditionfailFastConditionIf this condition if ever false, indicates our condition will never be true.private java.time.DurationpollDelayThe poll delay.private PollIntervalpollIntervalThe poll interval.private WaitConstrainttimeoutConstraintTiming constraint.
-
Constructor Summary
Constructors Constructor Description ConditionFactory(java.lang.String alias, WaitConstraint timeoutConstraint, PollInterval pollInterval, java.time.Duration pollDelay, boolean catchUncaughtExceptions, ExceptionIgnorer exceptionsIgnorer, ConditionEvaluationListener conditionEvaluationListener, ExecutorLifecycle executorLifecycle, FailFastCondition failFastCondition)Instantiates a new condition factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ConditionFactoryalias(java.lang.String alias)Set the aliasConditionFactoryand()A method to increase the readability of the Awaitility DSL.ConditionFactoryatLeast(long timeout, java.util.concurrent.TimeUnit unit)Condition has to be evaluated not earlier thantimeoutbefore throwing a timeout exception.ConditionFactoryatLeast(java.time.Duration timeout)Condition has to be evaluated not earlier thantimeoutbefore throwing a timeout exception.ConditionFactoryatMost(long timeout, java.util.concurrent.TimeUnit unit)Await at mosttimeoutbefore throwing a timeout exception.ConditionFactoryatMost(java.time.Duration timeout)Await at mosttimeoutbefore throwing a timeout exception.ConditionFactoryawait()Await for an asynchronous operation.ConditionFactoryawait(java.lang.String alias)Await for an asynchronous operation and give this await instance a particular name.ConditionFactorybetween(long atLeastDuration, java.util.concurrent.TimeUnit atLeastTimeUnit, long atMostDuration, java.util.concurrent.TimeUnit atMostTimeUnit)Specifies the duration window which has to be satisfied during operation execution.ConditionFactorybetween(java.time.Duration atLeast, java.time.Duration atMost)Specifies the duration window which has to be satisfied during operation execution.ConditionFactorycatchUncaughtExceptions()Instruct Awaitility to catch uncaught exceptions from other threads.ConditionFactoryconditionEvaluationListener(ConditionEvaluationListener conditionEvaluationListener)Handle condition evaluation results each time evaluation of a condition occurs.private java.time.DurationdefinePollDelay(java.time.Duration pollDelay, PollInterval pollInterval)Ensures backward compatibility (especially that poll delay is the same as poll interval for fixed poll interval).ConditionFactorydontCatchUncaughtExceptions()Don't catch uncaught exceptions in other threads.ConditionFactoryduring(long timeout, java.util.concurrent.TimeUnit unit)Await at the predicate holds during at leasttimeoutConditionFactoryduring(java.time.Duration timeout)Await at the predicate holds during at leasttimeoutConditionFactoryfailFast(java.lang.String failFastFailureReason, java.util.concurrent.Callable<java.lang.Boolean> failFastCondition)If the supplied Callable ever returns false, it indicates our condition will never be true, and if so fail the system immediately.ConditionFactoryfailFast(java.lang.String failFastFailureReason, ThrowingRunnable failFastAssertion)If the suppliedfailFastAssertionever returns throws an exception, it indicates our condition will never be true, and if so fail the system immediately.ConditionFactoryfailFast(java.util.concurrent.Callable<java.lang.Boolean> failFastCondition)If the supplied Callable ever returns false, it indicates our condition will never be true, and if so fail the system immediately.ConditionFactoryfailFast(ThrowingRunnable failFastAssertion)If the suppliedfailFastAssertionever returns throws an exception, it indicates our condition will never be true, and if so fail the system immediately.ConditionFactoryforever()Await forever until the condition is satisfied.private ConditionSettingsgenerateConditionSettings()ConditionFactorygiven()A method to increase the readability of the Awaitility DSL.ConditionFactoryignoreException(java.lang.Class<? extends java.lang.Throwable> exceptionType)Instruct Awaitility to ignore a specific exception and no subclasses of this exception.ConditionFactoryignoreExceptions()Instruct Awaitility to ignore all exceptions that occur during evaluation.ConditionFactoryignoreExceptionsInstanceOf(java.lang.Class<? extends java.lang.Throwable> exceptionType)Instruct Awaitility to ignore exceptions instance of the supplied exceptionType type.ConditionFactoryignoreExceptionsMatching(java.util.function.Predicate<? super java.lang.Throwable> predicate)Instruct Awaitility to ignore exceptions that occur during evaluation and matches the suppliedpredicate.ConditionFactoryignoreExceptionsMatching(org.hamcrest.Matcher<? super java.lang.Throwable> matcher)Instruct Awaitility to ignore exceptions that occur during evaluation and matches the supplied Hamcrest matcher.ConditionFactoryignoreNoExceptions()Instruct Awaitility to not ignore any exceptions that occur during evaluation.ConditionFactorylogging()Logging condition evaluation results each time evaluation of a condition occurs to System.out.ConditionFactorylogging(java.util.function.Consumer<java.lang.String> logPrinter)Logging condition evaluation results each time evaluation of a condition occurs to chosen consumer.ConditionFactorypollDelay(long delay, java.util.concurrent.TimeUnit unit)Specify the delay that will be used before Awaitility starts polling for the result the first time.ConditionFactorypollDelay(java.time.Duration pollDelay)Specify the delay that will be used before Awaitility starts polling for the result the first time.ConditionFactorypollExecutorService(java.util.concurrent.ExecutorService executorService)Specify the executor service whose threads will be used to evaluate the poll condition in Awaitility.ConditionFactorypollInSameThread()Instructs Awaitility to execute the polling of the condition from the same as the test.ConditionFactorypollInterval(long pollInterval, java.util.concurrent.TimeUnit unit)Specify the polling interval Awaitility will use for this await statement.ConditionFactorypollInterval(java.time.Duration pollInterval)Specify the polling interval Awaitility will use for this await statement.ConditionFactorypollInterval(PollInterval pollInterval)ConditionFactorypollThread(java.util.function.Function<java.lang.Runnable,java.lang.Thread> threadSupplier)Specify a thread supplier whose thread will be used to evaluate the poll condition in Awaitility.ConditionFactorythen()A method to increase the readability of the Awaitility DSL.ConditionFactorytimeout(long timeout, java.util.concurrent.TimeUnit unit)Await at mosttimeoutbefore throwing a timeout exception.ConditionFactorytimeout(java.time.Duration timeout)Await at mosttimeoutbefore throwing a timeout exception.voiduntil(java.util.concurrent.Callable<java.lang.Boolean> conditionEvaluator)Await until aCallablereturnstrue.<T> Tuntil(java.util.concurrent.Callable<T> supplier, java.util.function.Predicate<? super T> predicate)Wait until the given supplier matches the supplied predicate.<T> Tuntil(java.util.concurrent.Callable<T> supplier, org.hamcrest.Matcher<? super T> matcher)Await until aCallablesupplies a value matching the specifiedMatcher.private <T> Tuntil(Condition<T> condition)voiduntilAccumulator(java.util.concurrent.atomic.DoubleAccumulator accumulator, java.util.function.Consumer<? super java.lang.Double> matcher)Await until a DoubleAccumulator is asserted by thematcherconsumer E.g.voiduntilAccumulator(java.util.concurrent.atomic.DoubleAccumulator accumulator, org.hamcrest.Matcher<? super java.lang.Double> matcher)Await until aDoubleAccumulatorhas a value matching the specifiedMatcher.voiduntilAccumulator(java.util.concurrent.atomic.LongAccumulator accumulator, java.util.function.Consumer<? super java.lang.Long> matcher)Await until a LongAccumulator is asserted by thematcherconsumer E.g.voiduntilAccumulator(java.util.concurrent.atomic.LongAccumulator accumulator, org.hamcrest.Matcher<? super java.lang.Long> matcher)Await until aLongAccumulatorhas a value matching the specifiedMatcher.voiduntilAdder(java.util.concurrent.atomic.DoubleAdder adder, java.util.function.Consumer<? super java.lang.Double> matcher)Await until a DoubleAdder is asserted by thematcherconsumer E.g.voiduntilAdder(java.util.concurrent.atomic.DoubleAdder adder, org.hamcrest.Matcher<? super java.lang.Double> matcher)Await until aDoubleAdderhas a value matching the specifiedMatcher.voiduntilAdder(java.util.concurrent.atomic.LongAdder adder, java.util.function.Consumer<? super java.lang.Long> matcher)Await until a LongAdder is asserted by thematcherconsumer E.g.voiduntilAdder(java.util.concurrent.atomic.LongAdder adder, org.hamcrest.Matcher<? super java.lang.Long> matcher)Await until aLongAdderhas a value matching the specifiedMatcher.<T> voiduntilAsserted(java.util.concurrent.Callable<T> supplier, java.util.function.Consumer<? super T> assertConsumer)Await until a callable returns a value that when passed to aConsumerends without throwing an exception.voiduntilAsserted(ThrowingRunnable assertion)Await until aRunnablesupplier execution passes (ends without throwing an exception).voiduntilAtomic(java.util.concurrent.atomic.AtomicBoolean atomic, java.util.function.Consumer<? super java.lang.Boolean> matcher)Await until a AtomicBoolean is asserted by thematcherconsumer E.g.voiduntilAtomic(java.util.concurrent.atomic.AtomicBoolean atomic, org.hamcrest.Matcher<? super java.lang.Boolean> matcher)Await until a Atomic variable has a value matching the specifiedMatcher.voiduntilAtomic(java.util.concurrent.atomic.AtomicInteger atomic, java.util.function.Consumer<? super java.lang.Integer> matcher)Await until a Atomic integer is asserted by thematcherconsumer E.g.java.lang.IntegeruntilAtomic(java.util.concurrent.atomic.AtomicInteger atomic, org.hamcrest.Matcher<? super java.lang.Integer> matcher)Await until a Atomic variable has a value matching the specifiedMatcher.voiduntilAtomic(java.util.concurrent.atomic.AtomicLong atomic, java.util.function.Consumer<? super java.lang.Long> matcher)Await until a Atomic long is asserted by thematcherconsumer E.g.java.lang.LonguntilAtomic(java.util.concurrent.atomic.AtomicLong atomic, org.hamcrest.Matcher<? super java.lang.Long> matcher)Await until a Atomic variable has a value matching the specifiedMatcher.<V> voiduntilAtomic(java.util.concurrent.atomic.AtomicReference<V> atomic, java.util.function.Consumer<? super V> matcher)Await until a Atomic variable is asserted by thematcherconsumer E.g.<V> VuntilAtomic(java.util.concurrent.atomic.AtomicReference<V> atomic, org.hamcrest.Matcher<? super V> matcher)Await until a Atomic variable has a value matching the specifiedMatcher.voiduntilFalse(java.util.concurrent.atomic.AtomicBoolean atomic)Await until a Atomic boolean becomes false.voiduntilTrue(java.util.concurrent.atomic.AtomicBoolean atomic)Await until a Atomic boolean becomes true.ConditionFactorywith()A method to increase the readability of the Awaitility DSL.
-
-
-
Field Detail
-
timeoutConstraint
private final WaitConstraint timeoutConstraint
Timing constraint.
-
pollInterval
private final PollInterval pollInterval
The poll interval.
-
catchUncaughtExceptions
private final boolean catchUncaughtExceptions
The catch uncaught exceptions.
-
exceptionsIgnorer
private final ExceptionIgnorer exceptionsIgnorer
The ignore exceptions.
-
alias
private final java.lang.String alias
The alias.
-
pollDelay
private final java.time.Duration pollDelay
The poll delay.
-
conditionEvaluationListener
private final ConditionEvaluationListener conditionEvaluationListener
The condition evaluation listener
-
executorLifecycle
private final ExecutorLifecycle executorLifecycle
The executor lifecycle
-
failFastCondition
private final FailFastCondition failFastCondition
If this condition if ever false, indicates our condition will never be true.
-
-
Constructor Detail
-
ConditionFactory
public ConditionFactory(java.lang.String alias, WaitConstraint timeoutConstraint, PollInterval pollInterval, java.time.Duration pollDelay, boolean catchUncaughtExceptions, ExceptionIgnorer exceptionsIgnorer, ConditionEvaluationListener conditionEvaluationListener, ExecutorLifecycle executorLifecycle, FailFastCondition failFastCondition)Instantiates a new condition factory.- Parameters:
alias- the aliastimeoutConstraint- the timeout constraintpollInterval- the poll intervalpollDelay- The poll delaycatchUncaughtExceptions- the catch uncaught exceptionsexceptionsIgnorer- Determine which exceptions that should ignoredconditionEvaluationListener- Determine which exceptions that should ignoredexecutorLifecycle- The executor service and the lifecycle of the executor service that'll be used to evaluate the condition during pollingfailFastCondition- If this condition if ever false, indicates our condition will never be true.
-
-
Method Detail
-
conditionEvaluationListener
public ConditionFactory conditionEvaluationListener(ConditionEvaluationListener conditionEvaluationListener)
Handle condition evaluation results each time evaluation of a condition occurs. Works only with a Hamcrest matcher-based condition.- Parameters:
conditionEvaluationListener- the condition evaluation listener- Returns:
- the condition factory
-
logging
public ConditionFactory logging()
Logging condition evaluation results each time evaluation of a condition occurs to System.out.- Returns:
- the condition factory
-
logging
public ConditionFactory logging(java.util.function.Consumer<java.lang.String> logPrinter)
Logging condition evaluation results each time evaluation of a condition occurs to chosen consumer.- Returns:
- the condition factory
-
timeout
public ConditionFactory timeout(java.time.Duration timeout)
Await at mosttimeoutbefore throwing a timeout exception.- Parameters:
timeout- the timeout- Returns:
- the condition factory
-
atMost
public ConditionFactory atMost(java.time.Duration timeout)
Await at mosttimeoutbefore throwing a timeout exception.- Parameters:
timeout- the timeout- Returns:
- the condition factory
-
during
public ConditionFactory during(java.time.Duration timeout)
Await at the predicate holds during at leasttimeout- Parameters:
timeout- the timeout- Returns:
- the condition factory
-
during
public ConditionFactory during(long timeout, java.util.concurrent.TimeUnit unit)
Await at the predicate holds during at leasttimeout- Parameters:
timeout- the timeoutunit- the unit- Returns:
- the condition factory
-
alias
public ConditionFactory alias(java.lang.String alias)
Set the alias- Parameters:
alias- alias- Returns:
- the condition factory
- See Also:
Awaitility.await(String)
-
atLeast
public ConditionFactory atLeast(java.time.Duration timeout)
Condition has to be evaluated not earlier thantimeoutbefore throwing a timeout exception.- Parameters:
timeout- the timeout- Returns:
- the condition factory
-
atLeast
public ConditionFactory atLeast(long timeout, java.util.concurrent.TimeUnit unit)
Condition has to be evaluated not earlier thantimeoutbefore throwing a timeout exception.- Parameters:
timeout- the timeoutunit- the unit- Returns:
- the condition factory
-
between
public ConditionFactory between(java.time.Duration atLeast, java.time.Duration atMost)
Specifies the duration window which has to be satisfied during operation execution. In case operation is executed beforeatLeastor afteratMosttimeout exception is thrown.- Parameters:
atLeast- lower part of execution windowatMost- upper part of execution window- Returns:
- the condition factory
-
between
public ConditionFactory between(long atLeastDuration, java.util.concurrent.TimeUnit atLeastTimeUnit, long atMostDuration, java.util.concurrent.TimeUnit atMostTimeUnit)
Specifies the duration window which has to be satisfied during operation execution. In case operation is executed beforeatLeastDurationor afteratMostDurationtimeout exception is thrown.- Parameters:
atLeastDuration- lower part of execution windowatMostDuration- upper part of execution window- Returns:
- the condition factory
-
forever
public ConditionFactory forever()
Await forever until the condition is satisfied. Caution: You can block subsequent tests and the entire build can hang indefinitely, it's recommended to always use a timeout.- Returns:
- the condition factory
-
pollInterval
public ConditionFactory pollInterval(java.time.Duration pollInterval)
Specify the polling interval Awaitility will use for this await statement. This means the frequency in which the condition is checked for completion.Note that the poll delay will be automatically set as to the same value as the interval (if using a
FixedPollInterval) unless it's specified explicitly usingpollDelay(Duration),pollDelay(long, TimeUnit)orpollDelay(java.time.Duration).- Parameters:
pollInterval- the poll interval- Returns:
- the condition factory
-
timeout
public ConditionFactory timeout(long timeout, java.util.concurrent.TimeUnit unit)
Await at mosttimeoutbefore throwing a timeout exception.- Parameters:
timeout- the timeoutunit- the unit- Returns:
- the condition factory
-
pollDelay
public ConditionFactory pollDelay(long delay, java.util.concurrent.TimeUnit unit)
Specify the delay that will be used before Awaitility starts polling for the result the first time. If you don't specify a poll delay explicitly it'll be the same as the poll interval.- Parameters:
delay- the delayunit- the unit- Returns:
- the condition factory
-
pollDelay
public ConditionFactory pollDelay(java.time.Duration pollDelay)
Specify the delay that will be used before Awaitility starts polling for the result the first time. If you don't specify a poll delay explicitly it'll be the same as the poll interval.- Parameters:
pollDelay- the poll delay- Returns:
- the condition factory
-
atMost
public ConditionFactory atMost(long timeout, java.util.concurrent.TimeUnit unit)
Await at mosttimeoutbefore throwing a timeout exception.- Parameters:
timeout- the timeoutunit- the unit- Returns:
- the condition factory
-
pollInterval
public ConditionFactory pollInterval(long pollInterval, java.util.concurrent.TimeUnit unit)
Specify the polling interval Awaitility will use for this await statement. This means the frequency in which the condition is checked for completion.
Note that the poll delay will be automatically set as to the same value as the interval unless it's specified explicitly usingpollDelay(Duration),pollDelay(long, TimeUnit)orpollDelay(java.time.Duration), or ConditionFactory#andWithPollDelay(long, TimeUnit)}. This is the same as creating aFixedPollInterval.- Parameters:
pollInterval- the poll intervalunit- the unit- Returns:
- the condition factory
- See Also:
FixedPollInterval
-
pollInterval
public ConditionFactory pollInterval(PollInterval pollInterval)
-
catchUncaughtExceptions
public ConditionFactory catchUncaughtExceptions()
Instruct Awaitility to catch uncaught exceptions from other threads. This is useful in multi-threaded systems when you want your test to fail regardless of which thread throwing the exception. Default istrue.- Returns:
- the condition factory
-
ignoreExceptionsInstanceOf
public ConditionFactory ignoreExceptionsInstanceOf(java.lang.Class<? extends java.lang.Throwable> exceptionType)
Instruct Awaitility to ignore exceptions instance of the supplied exceptionType type. Exceptions will be treated as evaluating tofalse. This is useful in situations where the evaluated conditions may temporarily throw exceptions.If you want to ignore a specific exceptionType then use
ignoreException(Class)- Parameters:
exceptionType- The exception type (hierarchy) to ignore- Returns:
- the condition factory
-
ignoreException
public ConditionFactory ignoreException(java.lang.Class<? extends java.lang.Throwable> exceptionType)
Instruct Awaitility to ignore a specific exception and no subclasses of this exception. Exceptions will be treated as evaluating tofalse. This is useful in situations where the evaluated conditions may temporarily throw exceptions.If you want to ignore a subtypes of this exception then use
ignoreExceptionsInstanceOf(Class)}- Parameters:
exceptionType- The exception type to ignore- Returns:
- the condition factory
-
ignoreExceptions
public ConditionFactory ignoreExceptions()
Instruct Awaitility to ignore all exceptions that occur during evaluation. Exceptions will be treated as evaluating tofalse. This is useful in situations where the evaluated conditions may temporarily throw exceptions.- Returns:
- the condition factory.
-
ignoreNoExceptions
public ConditionFactory ignoreNoExceptions()
Instruct Awaitility to not ignore any exceptions that occur during evaluation. This is only useful if Awaitility is configured to ignore exceptions by default but you want to have a different behavior for a single test case.- Returns:
- the condition factory.
-
ignoreExceptionsMatching
public ConditionFactory ignoreExceptionsMatching(org.hamcrest.Matcher<? super java.lang.Throwable> matcher)
Instruct Awaitility to ignore exceptions that occur during evaluation and matches the supplied Hamcrest matcher. Exceptions will be treated as evaluating tofalse. This is useful in situations where the evaluated conditions may temporarily throw exceptions.- Returns:
- the condition factory.
-
ignoreExceptionsMatching
public ConditionFactory ignoreExceptionsMatching(java.util.function.Predicate<? super java.lang.Throwable> predicate)
Instruct Awaitility to ignore exceptions that occur during evaluation and matches the suppliedpredicate. Exceptions will be treated as evaluating tofalse. This is useful in situations where the evaluated conditions may temporarily throw exceptions.- Returns:
- the condition factory.
-
await
public ConditionFactory await()
Await for an asynchronous operation. This method returns the sameConditionFactoryinstance and is used only to get a more fluent-like syntax.- Returns:
- the condition factory
-
await
public ConditionFactory await(java.lang.String alias)
Await for an asynchronous operation and give this await instance a particular name. This is useful in cases when you have several await statements in one test and you want to know which one that fails (the alias will be shown if a timeout exception occurs).- Parameters:
alias- the alias- Returns:
- the condition factory
-
and
public ConditionFactory and()
A method to increase the readability of the Awaitility DSL. It simply returns the same condition factory instance.- Returns:
- the condition factory
-
with
public ConditionFactory with()
A method to increase the readability of the Awaitility DSL. It simply returns the same condition factory instance.- Returns:
- the condition factory
-
then
public ConditionFactory then()
A method to increase the readability of the Awaitility DSL. It simply returns the same condition factory instance.- Returns:
- the condition factory
-
given
public ConditionFactory given()
A method to increase the readability of the Awaitility DSL. It simply returns the same condition factory instance.- Returns:
- the condition factory
-
dontCatchUncaughtExceptions
public ConditionFactory dontCatchUncaughtExceptions()
Don't catch uncaught exceptions in other threads. This will not make the await statement fail if exceptions occur in other threads.- Returns:
- the condition factory
-
pollExecutorService
public ConditionFactory pollExecutorService(java.util.concurrent.ExecutorService executorService)
Specify the executor service whose threads will be used to evaluate the poll condition in Awaitility. Note that the executor service must be shutdown manually!This is an advanced feature and it should only be used sparingly.
- Parameters:
executorService- The executor service that Awaitility will use when polling condition evaluations- Returns:
- the condition factory
-
pollThread
public ConditionFactory pollThread(java.util.function.Function<java.lang.Runnable,java.lang.Thread> threadSupplier)
Specify a thread supplier whose thread will be used to evaluate the poll condition in Awaitility. The supplier will be called only once and the thread it returns will be reused during all condition evaluations. This is an advanced feature and it should only be used sparingly.- Parameters:
threadSupplier- A supplier of the thread that Awaitility will use when polling- Returns:
- the condition factory
-
pollInSameThread
public ConditionFactory pollInSameThread()
Instructs Awaitility to execute the polling of the condition from the same as the test. This is an advanced feature and you should be careful when combining this with conditions that wait forever (or a long time) since Awaitility cannot interrupt the thread when it's using the same thread as the test. For safety you should always combine tests using this feature with a test framework specific timeout, for example in JUnit:- Returns:
- the condition factory
-
failFast
public ConditionFactory failFast(java.util.concurrent.Callable<java.lang.Boolean> failFastCondition)
If the supplied Callable ever returns false, it indicates our condition will never be true, and if so fail the system immediately. Throws aTerminalFailureExceptionif fail fast condition evaluates totrue. If you want to specify a more descriptive error message then usefailFast(String, Callable).- Parameters:
failFastCondition- The terminal failure condition- Returns:
- the condition factory
- See Also:
failFast(String, Callable)
-
failFast
public ConditionFactory failFast(java.lang.String failFastFailureReason, java.util.concurrent.Callable<java.lang.Boolean> failFastCondition)
If the supplied Callable ever returns false, it indicates our condition will never be true, and if so fail the system immediately. Throws aTerminalFailureExceptionif fail fast condition evaluates totrue.- Parameters:
failFastFailureReason- A descriptive reason why the fail fast condition has failed, will be included in theTerminalFailureExceptionthrown iffailFastConditionevaluates totrue.failFastCondition- The terminal failure condition- Returns:
- the condition factory
-
failFast
public ConditionFactory failFast(ThrowingRunnable failFastAssertion)
If the suppliedfailFastAssertionever returns throws an exception, it indicates our condition will never be true, and if so fail the system immediately. This allows you to use a more descriptive error message of why the fail-fast condition failed by doing e.g.:Workflow workflow = .. await() .atMost(1, MINUTES) .failFast(() -> assertThat(workflow.get("phase")).describedAs("Workflow failed. Last known state:\n" + workflow.toPrettyString()).isNotEqualTo("Failed"); .untilAsserted(...);- Parameters:
failFastAssertion- The terminal failure assertion- Returns:
- the condition factory
- See Also:
failFast(String, Callable)
-
failFast
public ConditionFactory failFast(java.lang.String failFastFailureReason, ThrowingRunnable failFastAssertion)
If the suppliedfailFastAssertionever returns throws an exception, it indicates our condition will never be true, and if so fail the system immediately. This allows you to use a more descriptive error message of why the fail-fast condition failed by doing e.g.:Workflow workflow = .. await() .atMost(1, MINUTES) .failFast("workflow failed", () -> assertThat(workflow.get("phase")).describedAs("Workflow failed. Last known state:\n" + workflow.toPrettyString()).isNotEqualTo("Failed"); .untilAsserted(...);- Parameters:
failFastFailureReason- A descriptive reason why the fail fast condition has failed, will be included in theTerminalFailureExceptionthrown iffailFastAssertionthrows an exception.failFastAssertion- The terminal failure assertion- Returns:
- the condition factory
- See Also:
failFast(String, Callable)
-
until
public <T> T until(java.util.concurrent.Callable<T> supplier, org.hamcrest.Matcher<? super T> matcher)Await until aCallablesupplies a value matching the specifiedMatcher. E.g.await().until(numberOfPersons(), is(greaterThan(2)));
where "numberOfPersons()" returns a standardCallable:private Callable<Integer> numberOfPersons() { return new Callable<Integer>() { public Integer call() { return personRepository.size(); } }; }
Using a genericCallableas done by using this version of "until" allows you to reuse the "numberOfPersons()" definition in multiple await statements. I.e. you can easily create another await statement (perhaps in a different test case) using e.g.await().until(numberOfPersons(), is(equalTo(6)));
- Type Parameters:
T- the generic type- Parameters:
supplier- the supplier that is responsible for getting the value that should be matched.matcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Returns:
- a T object.
- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
until
public <T> T until(java.util.concurrent.Callable<T> supplier, java.util.function.Predicate<? super T> predicate)Wait until the given supplier matches the supplied predicate. For example:await().until(myRepository::count, cnt -> cnt == 2);
- Type Parameters:
T- the generic type- Parameters:
supplier- The supplier that returns the object that will be evaluated by the predicate.predicate- The predicate that must match- Returns:
- a T object.
- Since:
- 3.1.1
-
untilAsserted
public void untilAsserted(ThrowingRunnable assertion)
Await until aRunnablesupplier execution passes (ends without throwing an exception). E.g. with Java 8:await().untilAsserted(() -> Assertions.assertThat(personRepository.size()).isEqualTo(6));
orawait().untilAsserted(() -> assertEquals(6, personRepository.size()));
This method is intended to benefit from lambda expressions introduced in Java 8. It allows to use standard AssertJ/FEST Assert assertions (by the way also standard JUnit/TestNG assertions) to test asynchronous calls and systems.AssertionErrorinstances thrown by the supplier are treated as an assertion failure and proper error message is propagated on timeout. Other exceptions are rethrown immediately as an execution errors.
While technically it is completely valid to use plain Runnable class in Java 7 code, the resulting expression is very verbose and can decrease the readability of the test case, e.g.await().untilAsserted(new Runnable() { public void run() { Assertions.assertThat(personRepository.size()).isEqualTo(6); } });
NOTE:
Be VERY careful so that you're not using this method incorrectly in languages (like Kotlin and Groovy) that doesn't disambiguate between aThrowingRunnablethat doesn't return anything (void) andCallablethat returns a value. For example in Kotlin you can do like this:await().untilAsserted { true == false }and the compiler won't complain with an error (as is the case in Java). If you were to execute this test in Kotlin it'll pass!- Parameters:
assertion- the supplier that is responsible for executing the assertion and throwing AssertionError on failure.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.- Since:
- 1.6.0
-
untilAsserted
public <T> void untilAsserted(java.util.concurrent.Callable<T> supplier, java.util.function.Consumer<? super T> assertConsumer)Await until a callable returns a value that when passed to aConsumerends without throwing an exception. E.g. with Java 8:public class MyBean { public String myFunction() { // Imagine stuff being executed in asynchrinously here and the result of this // operation is a string called "my value" return "my value" } } // Then in your test you can wait for the "myFunction" to return "my value" await().untilAsserted(myBean::myFunction, value -> Assertions.assertThat(value).isEqualTo("my value"));- Parameters:
supplier- the supplier that is responsible for executing the assertion and throwing AssertionError on failure.assertConsumer- The consumer that will assert that the supplied value is correct, or throw an exception.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.- Since:
- 4.3.2
-
untilAtomic
public java.lang.Integer untilAtomic(java.util.concurrent.atomic.AtomicInteger atomic, org.hamcrest.Matcher<? super java.lang.Integer> matcher)Await until a Atomic variable has a value matching the specifiedMatcher. E.g.await().untilAtomic(myAtomic, is(greaterThan(2)));
- Parameters:
atomic- the atomic variablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Returns:
- a
Integerobject. - Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public void untilAtomic(java.util.concurrent.atomic.AtomicInteger atomic, java.util.function.Consumer<? super java.lang.Integer> matcher)Await until a Atomic integer is asserted by thematcherconsumer E.g.await().untilAtomic(myAtomic, value -> Assertions.assertThat(value).isEqualTo(123));
- Parameters:
atomic- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public java.lang.Long untilAtomic(java.util.concurrent.atomic.AtomicLong atomic, org.hamcrest.Matcher<? super java.lang.Long> matcher)Await until a Atomic variable has a value matching the specifiedMatcher. E.g.await().untilAtomic(myAtomic, is(greaterThan(2)));
- Parameters:
atomic- the atomic variablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Returns:
- a
Longobject. - Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public void untilAtomic(java.util.concurrent.atomic.AtomicLong atomic, java.util.function.Consumer<? super java.lang.Long> matcher)Await until a Atomic long is asserted by thematcherconsumer E.g.await().untilAtomic(myAtomic, value -> Assertions.assertThat(value).isEqualTo(123L));
- Parameters:
atomic- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public void untilAtomic(java.util.concurrent.atomic.AtomicBoolean atomic, org.hamcrest.Matcher<? super java.lang.Boolean> matcher)Await until a Atomic variable has a value matching the specifiedMatcher. E.g.await().untilAtomic(myAtomic, is(greaterThan(2)));
- Parameters:
atomic- the atomic variablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public void untilAtomic(java.util.concurrent.atomic.AtomicBoolean atomic, java.util.function.Consumer<? super java.lang.Boolean> matcher)Await until a AtomicBoolean is asserted by thematcherconsumer E.g.await().untilAtomic(myAtomicBoolean, value -> Assertions.assertThat(value).isTrue());
- Parameters:
atomic- the atomic variablematcher- the matcher The consumer that validates that the atomic boolean value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilTrue
public void untilTrue(java.util.concurrent.atomic.AtomicBoolean atomic)
Await until a Atomic boolean becomes true.- Parameters:
atomic- the atomic variable- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilFalse
public void untilFalse(java.util.concurrent.atomic.AtomicBoolean atomic)
Await until a Atomic boolean becomes false.- Parameters:
atomic- the atomic variable- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAdder
public void untilAdder(java.util.concurrent.atomic.LongAdder adder, org.hamcrest.Matcher<? super java.lang.Long> matcher)Await until aLongAdderhas a value matching the specifiedMatcher. E.g.await().untilAdder(myLongAdder, is(greaterThan(2L)));
- Parameters:
adder- theLongAddervariablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAdder
public void untilAdder(java.util.concurrent.atomic.LongAdder adder, java.util.function.Consumer<? super java.lang.Long> matcher)Await until a LongAdder is asserted by thematcherconsumer E.g.await().untilAdder(myAdder, value -> Assertions.assertThat(value).isEqualTo(123L));
- Parameters:
adder- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAdder
public void untilAdder(java.util.concurrent.atomic.DoubleAdder adder, org.hamcrest.Matcher<? super java.lang.Double> matcher)Await until aDoubleAdderhas a value matching the specifiedMatcher. E.g.await().untilAdder(myDoubleAdder, is(greaterThan(2.0d)));
- Parameters:
adder- theDoubleAddervariablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAdder
public void untilAdder(java.util.concurrent.atomic.DoubleAdder adder, java.util.function.Consumer<? super java.lang.Double> matcher)Await until a DoubleAdder is asserted by thematcherconsumer E.g.await().untilAdder(myAdder, value -> Assertions.assertThat(value).isEqualTo(12.3d));
- Parameters:
adder- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAccumulator
public void untilAccumulator(java.util.concurrent.atomic.LongAccumulator accumulator, org.hamcrest.Matcher<? super java.lang.Long> matcher)Await until aLongAccumulatorhas a value matching the specifiedMatcher. E.g.await().untilAccumulator(myLongAccumulator, is(greaterThan(2L)));
- Parameters:
accumulator- theLongAccumulatorvariablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAccumulator
public void untilAccumulator(java.util.concurrent.atomic.LongAccumulator accumulator, java.util.function.Consumer<? super java.lang.Long> matcher)Await until a LongAccumulator is asserted by thematcherconsumer E.g.await().untilAccumulator(myAdder, value -> Assertions.assertThat(value).isEqualTo(123L));
- Parameters:
accumulator- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAccumulator
public void untilAccumulator(java.util.concurrent.atomic.DoubleAccumulator accumulator, org.hamcrest.Matcher<? super java.lang.Double> matcher)Await until aDoubleAccumulatorhas a value matching the specifiedMatcher. E.g.await().untilAccumulator(myDoubleAccumulator, is(greaterThan(2.0d)));
- Parameters:
accumulator- theDoubleAccumulatorvariablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAccumulator
public void untilAccumulator(java.util.concurrent.atomic.DoubleAccumulator accumulator, java.util.function.Consumer<? super java.lang.Double> matcher)Await until a DoubleAccumulator is asserted by thematcherconsumer E.g.await().untilAccumulator(myAdder, value -> Assertions.assertThat(value).isEqualTo(12.3d));
- Parameters:
accumulator- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public <V> V untilAtomic(java.util.concurrent.atomic.AtomicReference<V> atomic, org.hamcrest.Matcher<? super V> matcher)Await until a Atomic variable has a value matching the specifiedMatcher. E.g.await().untilAtomic(myAtomic, is(greaterThan(2)));
- Type Parameters:
V- a V object.- Parameters:
atomic- the atomic variablematcher- the matcher The hamcrest matcher that checks whether the condition is fulfilled.- Returns:
- a V object.
- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
untilAtomic
public <V> void untilAtomic(java.util.concurrent.atomic.AtomicReference<V> atomic, java.util.function.Consumer<? super V> matcher)Await until a Atomic variable is asserted by thematcherconsumer E.g.await().untilAtomic(myAtomic, value -> Assertions.assertThat(value).isEqualTo("something"));- Type Parameters:
V- a V object.- Parameters:
atomic- the atomic variablematcher- the matcher The consumer that validates that the atomic reference value is correct, or throws an exception- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
until
public void until(java.util.concurrent.Callable<java.lang.Boolean> conditionEvaluator)
Await until aCallablereturnstrue. This is method is not as generic as the other variants of "until" but it allows for a more precise and in some cases even more english-like syntax. E.g.await().until(numberOfPersonsIsEqualToThree());
where "numberOfPersonsIsEqualToThree()" returns a standardCallableof typeBoolean:private Callable<Boolean> numberOfPersons() { return new Callable<Boolean>() { public Boolean call() { return personRepository.size() == 3; } }; }- Parameters:
conditionEvaluator- the condition evaluator- Throws:
ConditionTimeoutException- If condition was not fulfilled within the given time period.
-
generateConditionSettings
private ConditionSettings generateConditionSettings()
-
until
private <T> T until(Condition<T> condition)
-
definePollDelay
private java.time.Duration definePollDelay(java.time.Duration pollDelay, PollInterval pollInterval)Ensures backward compatibility (especially that poll delay is the same as poll interval for fixed poll interval). It also make sure that poll delay isDuration.ZEROfor all other poll intervals if poll delay was not explicitly defined. If poll delay was explicitly defined the it will just be returned.- Parameters:
pollDelay- The poll delaypollInterval- The chosen (or default) poll interval- Returns:
- The poll delay to use
-
-