public class ConditionPoll extends Object
Multi threaded tests are notoriously difficult to write. You should almost NEVER (though there is one exception to this) simply sleep for a certain amount of time and then expect a condition to be met. First of all it makes your tests slow, and it also makes them somewhat platform dependent. Ideally you should wait on the condition and be notified when the condition is met.
Given that you're writing a test, the next best thing to this ideal is to poll for the condition. While this should be avoided in production code (where possible), it's not that important in unit tests. And it's usually a lot easier to do than setting up a condition variable or a CountDownLatch and triggering it appropriately.
Therefore the class has several utility methods for helping to write multithreaded tests by allowing easy polling for a particular condition for a fixed amount of time and returns the final condition value. For example:
import static net.dempsy.utils.test.ConditionPoll.poll;
import static org.junit.Assert.assertTrue;
...
public int numOfTimesSomethingHappens = 0;
....
new Thread(() -> { doSomethingThatIncrementsNumOfTimesSomethingHappensAndExit() }).start();
...
assertTrue(poll(() -> numOfTimesSomethingHappens == numTimesExpected));
Modifier and Type | Class and Description |
---|---|
static interface |
ConditionPoll.Condition<T>
This is the interface that serves as the root for anonymous classes passed to the poll call.
|
Modifier and Type | Field and Description |
---|---|
static long |
baseTimeoutMillis
The default polling timeout.
|
Constructor and Description |
---|
ConditionPoll() |
Modifier and Type | Method and Description |
---|---|
static void |
assertTrue(Supplier<String> errMessage,
boolean value) |
static boolean |
poll(ConditionPoll.Condition condition)
Poll for a given condition for
baseTimeoutMillis milliseconds. |
static <T> boolean |
poll(long timeoutMillis,
T userObject,
ConditionPoll.Condition<T> condition)
Poll for a given condition for timeoutMillis milliseconds.
|
static <T> boolean |
poll(T userObject,
ConditionPoll.Condition<T> condition)
Poll for a given condition for
baseTimeoutMillis milliseconds. |
static <T> boolean |
qpoll(T userObject,
ConditionPoll.Condition<T> condition) |
public static final long baseTimeoutMillis
public static <T> boolean poll(long timeoutMillis, T userObject, ConditionPoll.Condition<T> condition) throws InterruptedException
Poll for a given condition for timeoutMillis milliseconds. If the condition hasn't been met by then return false. Otherwise, return true as soon as the condition is met.
Anything passed to as the userObject will be passed on to the ConditionPoll.Condition
and is for the implementor to use as they see fit.
InterruptedException
public static <T> boolean poll(T userObject, ConditionPoll.Condition<T> condition) throws InterruptedException
Poll for a given condition for baseTimeoutMillis
milliseconds. If the condition hasn't been met by then return false. Otherwise, return true as soon as the condition is met.
Anything passed to as the userObject will be passed on to the ConditionPoll.Condition
and is for the implementor to use as they see fit.
InterruptedException
public static <T> boolean qpoll(T userObject, ConditionPoll.Condition<T> condition)
public static boolean poll(ConditionPoll.Condition condition) throws InterruptedException
Poll for a given condition for baseTimeoutMillis
milliseconds. If the condition hasn't been met by then return false. Otherwise, return true as soon as the condition is met.
InterruptedException
Copyright © 2018. All rights reserved.