java.lang.Object
com.inductiveautomation.ignition.common.util.TestUtil

public class TestUtil extends Object
Helpers for writing tests. These are mostly small functions that end up getting written a lot when creating unit tests.
  • Method Details

    • sleepSafe

      public static void sleepSafe(long ms)
      Thread.sleep without the exception.
    • runAsync

      public static CompletableFuture<Void> runAsync(Runnable r)
      Runs the runnable in a new thread, providing a CompletableFuture that can be used to wait for completion.
    • runAsyncSupplier

      public static <T> CompletableFuture<T> runAsyncSupplier(Supplier<T> r)
      Runs the task in a new thread, returning a result through the CompletableFuture
    • setConditionTimeoutMS

      public static void setConditionTimeoutMS(int timeoutMS)
      Sets the timeout, in milliseconds, that will be used for all condition wait activities.
    • enableCondition

      public static void enableCondition(String keyName)
      Creates a condition with the given name that can be used to control flow. A "condition" is a flow control unit, inspired by Condition, 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:
      1. waitCondition is injected in the code we want to test, for the applicable key.
      2. In our test, we start that code asynchronously.
      3. In our test, we call ensureConditionWaiting to wait until the async code reaches the wait
      4. When we want, we call releaseCondition to allow the async code to continue
      Keep in mind that all the waits are governed/limited by the ConditionTimeoutMS
      This system does not expect multiple threads to wait on the same condition.
      All flags (waiting, released) are cleared when waitCondition returns.
    • waitCondition

      public static void waitCondition(String keyName)
      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

      public static void ensureConditionWaiting(String keyName)
      Blocks until another thread is waiting on waitCondition. See enableCondition for more information.
    • releaseCondition

      public static void releaseCondition(String keyName)
      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()