@Test
public void handlePotentiallyStuckInterruptsThreadWhenListenerReturnsTrue() throws InterruptedException {

  Duration processingTime = standardHours(1);
  when(listener1.handlePotentiallyStuck(any(ListenerContext.class), eq(processingTime))).thenReturn(true);
  WorkflowInstance instance = executingInstanceBuilder().setType("stuck").setState("start").build();
  when(workflowInstances.getWorkflowInstance(instance.id, INCLUDES, null)).thenReturn(instance);
  Thread thread = new Thread(executor::run);
  thread.start();
  // let the workflow process until it is in stuck in the sleep
  sleep(500);
  executor.handlePotentiallyStuck(processingTime);
  thread.join(1000);
  assertFalse(thread.isAlive(), "Processing thread did not die after interruption");
  verify(listener1).handlePotentiallyStuck(any(ListenerContext.class), eq(processingTime));
  verify(listener2).handlePotentiallyStuck(any(ListenerContext.class), eq(processingTime));
  verify(workflowInstanceDao).updateWorkflowInstanceAfterExecution(update.capture(), action.capture(), childWorkflows.capture(),
          workflows.capture(), eq(true));
  assertThat(action.getValue().type, is(stateExecutionFailed));
  assertThat(action.getValue().stateText, containsString("InterruptedException"));
}