Class BlockingMessageInterceptor

java.lang.Object
org.jgroups.raft.testfwk.BlockingMessageInterceptor

@ThreadSafe public final class BlockingMessageInterceptor extends Object
Blocks while handling a message.

The interceptor receives a predicate to verify on each message. In case the predicate evaluates to true, the caller decides whether to block the message. This mechanism is useful to create breakpoints at specific points of an algorithm or message exchange. This utilization creates more contrived scenarios, or manually creates specific sequence of events.

Blocking with the interceptor adds the message to a queue and blocks until another thread intervenes and releases the message. In case of synchronous execution with the MockRaftCluster, the method blocks the invoking thread. In cases the cluster is asynchronous, the message is delayed until delivered. The thread remains blocked or the message is delayed until it is released by another thread. However, we include a hardcoded 60 seconds timeout to avoid threads leaking during the test execution.

Additionally, the interceptor include some helper assertion methods, to validate the queued operations.

Since:
1.0.13
Author:
José Bolina
  • Constructor Details

    • BlockingMessageInterceptor

      public BlockingMessageInterceptor(Predicate<org.jgroups.Message> predicate)
  • Method Details

    • shouldBlock

      public boolean shouldBlock(org.jgroups.Message message)
      Check if the given message should be blocked.
      Parameters:
      message - Message to check the headers.
      Returns:
      true if the message blocks. false, otherwise.
    • blockMessage

      public void blockMessage(org.jgroups.Message message, boolean async, Runnable onComplete)
      Blocks or delay the sending of the given message.

      Invoking this method will block the invoking thread if operating synchronously. The asynchronous execution simulates a delay in the message delivery and does not block the invoking thread.

      Parameters:
      message - Message identified to block.
      async - Whether to block the thread or introduce an asynchronous delay.
      onComplete - Block to run after the asynchronous delay finishes. Must be non-null in case is true.
      Throws:
      RuntimeException - An unchecked exception in case the thread is interrupted.
      AssertionError - If the execution is async and there is no runnable to run on completion.
    • blockMessage

      public void blockMessage(org.jgroups.Message message)
      Blocks the invoking thread while sending the given message.

      Invoking this method will block the invoking thread.

      Parameters:
      message - Message identified to block.
      See Also:
    • releaseNext

      public void releaseNext()
      Releases the next blocked message in line.
      Throws:
      IllegalStateException - In case there is no blocked message.
    • assertNumberOfBlockedMessages

      public void assertNumberOfBlockedMessages(int size)
      Helper to assert an expected number of blocked messages.
      Parameters:
      size - Expected number of blocked messages.
      Throws:
      AssertionError - In case the actual number of blocked messages is different.
    • assertNoBlockedMessages

      public void assertNoBlockedMessages()
      Helper to assert there is no more blocked messages.
      Throws:
      AssertionError - In case there is still blocked messages.
    • numberOfBlockedMessages

      public int numberOfBlockedMessages()
      The number of blocked messages.
      Returns:
      The number of blocked messages.