Class OneShotLatch

java.lang.Object
org.apache.flink.core.testutils.OneShotLatch

public final class OneShotLatch extends Object
Latch for synchronizing parts of code in tests. Once the latch has fired once calls to await() will return immediately in the future.

A part of the code that should only run after other code calls await(). The call will only return once the other part is finished and calls trigger().

  • Constructor Details

    • OneShotLatch

      public OneShotLatch()
  • Method Details

    • trigger

      public void trigger()
      Fires the latch. Code that is blocked on await() will now return.
    • await

      public void await() throws InterruptedException
      Waits until trigger() is called. Once trigger() has been called this call will always return immediately.
      Throws:
      InterruptedException - Thrown if the thread is interrupted while waiting.
    • awaitQuietly

      public void awaitQuietly()
      Calls await() and transforms any InterruptedException into a RuntimeException.
    • await

      public void await(long timeout, TimeUnit timeUnit) throws InterruptedException, TimeoutException
      Waits until trigger() is called. Once #trigger() has been called this call will always return immediately.

      If the latch is not triggered within the given timeout, a TimeoutException will be thrown after the timeout.

      A timeout value of zero means infinite timeout and make this equivalent to await().

      Parameters:
      timeout - The value of the timeout, a value of zero indicating infinite timeout.
      timeUnit - The unit of the timeout
      Throws:
      InterruptedException - Thrown if the thread is interrupted while waiting.
      TimeoutException - Thrown, if the latch is not triggered within the timeout time.
    • awaitQuietly

      public void awaitQuietly(long timeout, TimeUnit timeUnit)
    • isTriggered

      public boolean isTriggered()
      Checks if the latch was triggered.
      Returns:
      True, if the latch was triggered, false if not.
    • getWaitersCount

      public int getWaitersCount()
    • reset

      public void reset()
      Resets the latch so that isTriggered() returns false.
    • toString

      public String toString()
      Overrides:
      toString in class Object