Class TestUtil
java.lang.Object
com.inductiveautomation.ignition.common.util.TestUtil
Helpers for writing tests. These are mostly small functions that end up getting written a lot when
creating unit tests.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic void
enableCondition
(String keyName) Creates a condition with the given name that can be used to control flow.static void
ensureConditionWaiting
(String keyName) Blocks until another thread is waiting on waitCondition.static void
static void
releaseCondition
(String keyName) Releases a thread that is waiting on waitCondition.static CompletableFuture<Void>
Runs the runnable in a new thread, providing aCompletableFuture
that can be used to wait for completion.static <T> CompletableFuture<T>
runAsyncSupplier
(Supplier<T> r) Runs the task in a new thread, returning a result through theCompletableFuture
static void
setConditionTimeoutMS
(int timeoutMS) Sets the timeout, in milliseconds, that will be used for all condition wait activities.static void
sleepSafe
(long ms) Thread.sleep without the exception.static void
waitCondition
(String keyName) Will wait for the condition to be released, if present.
-
Method Details
-
sleepSafe
public static void sleepSafe(long ms) Thread.sleep without the exception. -
runAsync
Runs the runnable in a new thread, providing aCompletableFuture
that can be used to wait for completion. -
runAsyncSupplier
Runs the task in a new thread, returning a result through theCompletableFuture
-
setConditionTimeoutMS
public static void setConditionTimeoutMS(int timeoutMS) Sets the timeout, in milliseconds, that will be used for all condition wait activities. -
enableCondition
Creates a condition with the given name that can be used to control flow. A "condition" is a flow control unit, inspired byCondition
, but a little specialized for our purposes. We track the wait/released state to avoid a race condition for the release action (in other words, if releaseCondition is called before waitCondition, then waitCondition will return immediately).
However, we also have ensureConditionWaiting to help with flow, and it will block until waitCondition is called.
Therefore, the normal pattern is:- waitCondition is injected in the code we want to test, for the applicable key.
- In our test, we start that code asynchronously.
- In our test, we call ensureConditionWaiting to wait until the async code reaches the wait
- When we want, we call releaseCondition to allow the async code to continue
This system does not expect multiple threads to wait on the same condition.
All flags (waiting, released) are cleared when waitCondition returns. -
waitCondition
Will wait for the condition to be released, if present. See enableCondition for more information. It is not expected that more than one thread will be waiting on the waitCondition. This function may return immediately if releaseCondition was called before. All flags (wait, released) are reset when this function is called, so it may be used multiple times. -
ensureConditionWaiting
Blocks until another thread is waiting on waitCondition. See enableCondition for more information. -
releaseCondition
Releases a thread that is waiting on waitCondition. It is not expected that more than one thread is waiting on the condition. If called before waitCondition, a flag will be set to allow waitCondition to pass when called. See enableCondition for more information. -
releaseAllConditions
public static void releaseAllConditions()
-