Constructor and Description |
---|
ConditionFactory(String alias,
WaitConstraint timeoutConstraint,
PollInterval pollInterval,
Duration pollDelay,
boolean catchUncaughtExceptions,
ExceptionIgnorer exceptionsIgnorer,
ConditionEvaluationListener conditionEvaluationListener,
ExecutorLifecycle executorLifecycle)
Instantiates a new condition factory.
|
Modifier and Type | Method and Description |
---|---|
ConditionFactory |
alias(String alias)
Set the alias
|
ConditionFactory |
and()
A method to increase the readability of the Awaitility DSL.
|
ConditionFactory |
atLeast(Duration timeout)
Condition has to be evaluated not earlier than
timeout before throwing a timeout exception. |
ConditionFactory |
atLeast(long timeout,
TimeUnit unit)
Condition has to be evaluated not earlier than
timeout before throwing a timeout exception. |
ConditionFactory |
atMost(Duration timeout)
Await at most
timeout before throwing a timeout exception. |
ConditionFactory |
atMost(long timeout,
TimeUnit unit)
Await at most
timeout before throwing a timeout exception. |
ConditionFactory |
await()
Await for an asynchronous operation.
|
ConditionFactory |
await(String alias)
Await for an asynchronous operation and give this await instance a
particular name.
|
ConditionFactory |
between(Duration atLeast,
Duration atMost)
Specifies the duration window which has to be satisfied during operation execution.
|
ConditionFactory |
between(long atLeastDuration,
TimeUnit atLeastTimeUnit,
long atMostDuration,
TimeUnit atMostTimeUnit)
Specifies the duration window which has to be satisfied during operation execution.
|
ConditionFactory |
catchUncaughtExceptions()
Instruct Awaitility to catch uncaught exceptions from other threads.
|
ConditionFactory |
conditionEvaluationListener(ConditionEvaluationListener conditionEvaluationListener)
Handle condition evaluation results each time evaluation of a condition occurs.
|
ConditionFactory |
dontCatchUncaughtExceptions()
Don't catch uncaught exceptions in other threads.
|
ConditionFactory |
during(Duration timeout)
Await at the predicate holds during at least
timeout |
ConditionFactory |
during(long timeout,
TimeUnit unit)
Await at the predicate holds during at least
timeout |
ConditionFactory |
forever()
Await forever until the condition is satisfied.
|
ConditionFactory |
given()
A method to increase the readability of the Awaitility DSL.
|
ConditionFactory |
ignoreException(Class<? extends Throwable> exceptionType)
Instruct Awaitility to ignore a specific exception and no subclasses of this exception.
|
ConditionFactory |
ignoreExceptions()
Instruct Awaitility to ignore all exceptions that occur during evaluation.
|
ConditionFactory |
ignoreExceptionsInstanceOf(Class<? extends Throwable> exceptionType)
Instruct Awaitility to ignore exceptions instance of the supplied exceptionType type.
|
ConditionFactory |
ignoreExceptionsMatching(org.hamcrest.Matcher<? super Throwable> matcher)
Instruct Awaitility to ignore exceptions that occur during evaluation and matches the supplied Hamcrest matcher.
|
ConditionFactory |
ignoreExceptionsMatching(Predicate<? super Throwable> predicate)
Instruct Awaitility to ignore exceptions that occur during evaluation and matches the supplied
predicate . |
ConditionFactory |
ignoreNoExceptions()
Instruct Awaitility to not ignore any exceptions that occur during evaluation.
|
ConditionFactory |
pollDelay(Duration pollDelay)
Specify the delay that will be used before Awaitility starts polling for
the result the first time.
|
ConditionFactory |
pollDelay(long delay,
TimeUnit unit)
Specify the delay that will be used before Awaitility starts polling for
the result the first time.
|
ConditionFactory |
pollExecutorService(ExecutorService executorService)
Specify the executor service whose threads will be used to evaluate the poll condition in Awaitility.
|
ConditionFactory |
pollInSameThread()
Instructs Awaitility to execute the polling of the condition from the same as the test.
|
ConditionFactory |
pollInterval(Duration pollInterval)
Specify the polling interval Awaitility will use for this await
statement.
|
ConditionFactory |
pollInterval(long pollInterval,
TimeUnit unit)
Specify the polling interval Awaitility will use for this await
statement.
|
ConditionFactory |
pollInterval(PollInterval pollInterval) |
ConditionFactory |
pollThread(Function<Runnable,Thread> threadSupplier)
Specify a thread supplier whose thread will be used to evaluate the poll condition in Awaitility.
|
ConditionFactory |
then()
A method to increase the readability of the Awaitility DSL.
|
ConditionFactory |
timeout(Duration timeout)
Await at most
timeout before throwing a timeout exception. |
ConditionFactory |
timeout(long timeout,
TimeUnit unit)
Await at most
timeout before throwing a timeout exception. |
void |
until(Callable<Boolean> conditionEvaluator)
Await until a
Callable returns true . |
<T> T |
until(Callable<T> supplier,
org.hamcrest.Matcher<? super T> matcher)
Await until a
Callable supplies a value matching the specified
Matcher . |
<T> T |
until(Callable<T> supplier,
Predicate<? super T> predicate)
Wait until the given supplier matches the supplied predicate.
|
void |
untilAccumulator(DoubleAccumulator accumulator,
org.hamcrest.Matcher<? super Double> matcher)
Await until a
DoubleAccumulator has a value matching the specified Matcher . |
void |
untilAccumulator(LongAccumulator accumulator,
org.hamcrest.Matcher<? super Long> matcher)
Await until a
LongAccumulator has a value matching the specified Matcher . |
void |
untilAdder(DoubleAdder adder,
org.hamcrest.Matcher<? super Double> matcher)
Await until a
DoubleAdder has a value matching the specified Matcher . |
void |
untilAdder(LongAdder adder,
org.hamcrest.Matcher<? super Long> matcher)
Await until a
LongAdder has a value matching the specified Matcher . |
void |
untilAsserted(ThrowingRunnable assertion)
Await until a
Runnable supplier execution passes (ends without throwing an exception). |
void |
untilAtomic(AtomicBoolean atomic,
org.hamcrest.Matcher<? super Boolean> matcher)
Await until a Atomic variable has a value matching the specified
Matcher . |
Integer |
untilAtomic(AtomicInteger atomic,
org.hamcrest.Matcher<? super Integer> matcher)
Await until a Atomic variable has a value matching the specified
Matcher . |
Long |
untilAtomic(AtomicLong atomic,
org.hamcrest.Matcher<? super Long> matcher)
Await until a Atomic variable has a value matching the specified
Matcher . |
<V> V |
untilAtomic(AtomicReference<V> atomic,
org.hamcrest.Matcher<? super V> matcher)
Await until a Atomic variable has a value matching the specified
Matcher . |
void |
untilFalse(AtomicBoolean atomic)
Await until a Atomic boolean becomes false.
|
void |
untilTrue(AtomicBoolean atomic)
Await until a Atomic boolean becomes true.
|
ConditionFactory |
with()
A method to increase the readability of the Awaitility DSL.
|
public ConditionFactory(String alias, WaitConstraint timeoutConstraint, PollInterval pollInterval, Duration pollDelay, boolean catchUncaughtExceptions, ExceptionIgnorer exceptionsIgnorer, ConditionEvaluationListener conditionEvaluationListener, ExecutorLifecycle executorLifecycle)
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 pollingpublic ConditionFactory conditionEvaluationListener(ConditionEvaluationListener conditionEvaluationListener)
conditionEvaluationListener
- the condition evaluation listenerpublic ConditionFactory timeout(Duration timeout)
timeout
before throwing a timeout exception.timeout
- the timeoutpublic ConditionFactory atMost(Duration timeout)
timeout
before throwing a timeout exception.timeout
- the timeoutpublic ConditionFactory during(Duration timeout)
timeout
timeout
- the timeoutpublic ConditionFactory during(long timeout, TimeUnit unit)
timeout
timeout
- the timeoutunit
- the unitpublic ConditionFactory alias(String alias)
alias
- aliasAwaitility.await(String)
public ConditionFactory atLeast(Duration timeout)
timeout
before throwing a timeout exception.timeout
- the timeoutpublic ConditionFactory atLeast(long timeout, TimeUnit unit)
timeout
before throwing a timeout exception.timeout
- the timeoutunit
- the unitpublic ConditionFactory between(Duration atLeast, Duration atMost)
atLeast
or after atMost
timeout exception is thrown.atLeast
- lower part of execution windowatMost
- upper part of execution windowpublic ConditionFactory between(long atLeastDuration, TimeUnit atLeastTimeUnit, long atMostDuration, TimeUnit atMostTimeUnit)
atLeastDuration
or after atMostDuration
timeout exception is thrown.atLeastDuration
- lower part of execution windowatMostDuration
- upper part of execution windowpublic ConditionFactory forever()
public ConditionFactory pollInterval(Duration pollInterval)
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 using
pollDelay(Duration)
, pollDelay(long, TimeUnit)
or
pollDelay(java.time.Duration)
.
pollInterval
- the poll intervalpublic ConditionFactory timeout(long timeout, TimeUnit unit)
timeout
before throwing a timeout exception.timeout
- the timeoutunit
- the unitpublic ConditionFactory pollDelay(long delay, TimeUnit unit)
delay
- the delayunit
- the unitpublic ConditionFactory pollDelay(Duration pollDelay)
pollDelay
- the poll delaypublic ConditionFactory atMost(long timeout, TimeUnit unit)
timeout
before throwing a timeout exception.timeout
- the timeoutunit
- the unitpublic ConditionFactory pollInterval(long pollInterval, TimeUnit unit)
Note that the poll delay will be automatically set as to the same value as the interval unless it's specified explicitly using
pollDelay(Duration)
, pollDelay(long, TimeUnit)
or
pollDelay(java.time.Duration)
, or
ConditionFactory#andWithPollDelay(long, TimeUnit)}. This is the same as creating a FixedPollInterval
.pollInterval
- the poll intervalunit
- the unitFixedPollInterval
public ConditionFactory pollInterval(PollInterval pollInterval)
public ConditionFactory catchUncaughtExceptions()
true
.public ConditionFactory ignoreExceptionsInstanceOf(Class<? extends Throwable> exceptionType)
false
.
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)
exceptionType
- The exception type (hierarchy) to ignorepublic ConditionFactory ignoreException(Class<? extends Throwable> exceptionType)
false
.
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)
}
exceptionType
- The exception type to ignorepublic ConditionFactory ignoreExceptions()
false
. This is useful in situations where the evaluated
conditions may temporarily throw exceptions.public ConditionFactory ignoreNoExceptions()
public ConditionFactory ignoreExceptionsMatching(org.hamcrest.Matcher<? super Throwable> matcher)
false
. This is useful in situations where the evaluated conditions may temporarily throw exceptions.public ConditionFactory ignoreExceptionsMatching(Predicate<? super Throwable> predicate)
predicate
.
Exceptions will be treated as evaluating to
false
. This is useful in situations where the evaluated conditions may temporarily throw exceptions.public ConditionFactory await()
ConditionFactory
instance and is used only to get a more
fluent-like syntax.public ConditionFactory await(String alias)
alias
- the aliaspublic ConditionFactory and()
public ConditionFactory with()
public ConditionFactory then()
public ConditionFactory given()
public ConditionFactory dontCatchUncaughtExceptions()
public ConditionFactory pollExecutorService(ExecutorService executorService)
executorService
- The executor service that Awaitility will use when polling condition evaluationspublic ConditionFactory pollThread(Function<Runnable,Thread> threadSupplier)
threadSupplier
- A supplier of the thread that Awaitility will use when pollingpublic ConditionFactory pollInSameThread()
public <T> T until(Callable<T> supplier, org.hamcrest.Matcher<? super T> matcher)
Callable
supplies a value matching the specified
Matcher
. E.g.
await().until(numberOfPersons(), is(greaterThan(2)));
where "numberOfPersons()" returns a standard
Callable
:
private Callable<Integer> numberOfPersons() { return new Callable<Integer>() { public Integer call() { return personRepository.size(); } }; }
Using a generic
Callable
as 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)));
T
- the generic typesupplier
- 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.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public <T> T until(Callable<T> supplier, Predicate<? super T> predicate)
await().until(myRepository::count, cnt -> cnt == 2);
T
- the generic typesupplier
- The supplier that returns the object that will be evaluated by the predicate.predicate
- The predicate that must matchpublic void untilAsserted(ThrowingRunnable assertion)
Runnable
supplier execution passes (ends without throwing an exception). E.g. with Java 8:
await().untilAsserted(() -> Assertions.assertThat(personRepository.size()).isEqualTo(6));or
await().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.
AssertionError
instances 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:
ThrowingRunnable
that doesn't return anything (void) and Callable
that 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!
assertion
- the supplier that is responsible for executing the assertion and throwing AssertionError on failure.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public Integer untilAtomic(AtomicInteger atomic, org.hamcrest.Matcher<? super Integer> matcher)
Matcher
. E.g.
await().untilAtomic(myAtomic, is(greaterThan(2)));
atomic
- the atomic variablematcher
- the matcher The hamcrest matcher that checks whether the
condition is fulfilled.Integer
object.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public Long untilAtomic(AtomicLong atomic, org.hamcrest.Matcher<? super Long> matcher)
Matcher
. E.g.
await().untilAtomic(myAtomic, is(greaterThan(2)));
atomic
- the atomic variablematcher
- the matcher The hamcrest matcher that checks whether the
condition is fulfilled.Long
object.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilAtomic(AtomicBoolean atomic, org.hamcrest.Matcher<? super Boolean> matcher)
Matcher
. E.g.
await().untilAtomic(myAtomic, is(greaterThan(2)));
atomic
- the atomic variablematcher
- the matcher The hamcrest matcher that checks whether the
condition is fulfilled.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilTrue(AtomicBoolean atomic)
atomic
- the atomic variableConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilFalse(AtomicBoolean atomic)
atomic
- the atomic variableConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilAdder(LongAdder adder, org.hamcrest.Matcher<? super Long> matcher)
LongAdder
has a value matching the specified Matcher
. E.g.
await().untilAdder(myLongAdder, is(greaterThan(2L)));
adder
- the LongAdder
variablematcher
- the matcher The hamcrest matcher that checks whether the condition is fulfilled.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilAdder(DoubleAdder adder, org.hamcrest.Matcher<? super Double> matcher)
DoubleAdder
has a value matching the specified Matcher
. E.g.
await().untilAdder(myDoubleAdder, is(greaterThan(2.0d)));
adder
- the DoubleAdder
variablematcher
- the matcher The hamcrest matcher that checks whether the condition is fulfilled.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilAccumulator(LongAccumulator accumulator, org.hamcrest.Matcher<? super Long> matcher)
LongAccumulator
has a value matching the specified Matcher
. E.g.
await().untilAccumulator(myLongAccumulator, is(greaterThan(2L)));
accumulator
- the LongAccumulator
variablematcher
- the matcher The hamcrest matcher that checks whether the condition is fulfilled.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public void untilAccumulator(DoubleAccumulator accumulator, org.hamcrest.Matcher<? super Double> matcher)
DoubleAccumulator
has a value matching the specified Matcher
. E.g.
await().untilAccumulator(myDoubleAccumulator, is(greaterThan(2.0d)));
accumulator
- the DoubleAccumulator
variablematcher
- the matcher The hamcrest matcher that checks whether the condition is fulfilled.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public <V> V untilAtomic(AtomicReference<V> atomic, org.hamcrest.Matcher<? super V> matcher)
Matcher
. E.g.
await().untilAtomic(myAtomic, is(greaterThan(2)));
V
- a V object.atomic
- the atomic variablematcher
- the matcher The hamcrest matcher that checks whether the
condition is fulfilled.ConditionTimeoutException
- If condition was not fulfilled within the given time period.public void until(Callable<Boolean> conditionEvaluator)
Callable
returns true
. 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 standard
Callable
of type Boolean
:
private Callable<Boolean> numberOfPersons() { return new Callable<Boolean>() { public Boolean call() { return personRepository.size() == 3; } }; }
conditionEvaluator
- the condition evaluatorConditionTimeoutException
- If condition was not fulfilled within the given time period.Copyright © 2010–2020. All rights reserved.