@Test
public void testJobBatchDelayUpdate() {

    ScheduledFuture<?> future = Mockito.mock(ScheduledFuture.class);
    AtomicReference<Runnable> runnableToInvoke = new AtomicReference<>();
    when(scheduledExecutor.schedule(isA(Runnable.class), anyLong(), any())).then((invoc) -> {
        runnableToInvoke.set(((Runnable) invoc.getArgument(0)));
        return future;
    });
    Duration startDelay = Duration.ofMillis(10);
    Duration interBatchDelay = Duration.ofMillis(5);
    AtomicReference<BatchJobExecutorService.BatchJob<Integer>> job = new AtomicReference<>();
    AtomicLong counter = new AtomicLong();
    job.set(batchExecutor.startJob((batchSize) -> {
        switch ((int) counter.incrementAndGet()) {
            case 1:
                return false;
            case 2:
                job.get().setInterBatchDelay(Duration.ofNanos(1234L));
                return false;
            default:
                return true;
        }
    }, 42, startDelay, interBatchDelay));
    verify(scheduledExecutor, times(1)).schedule(isA(Runnable.class), eq(startDelay.toNanos()), eq(TimeUnit.NANOSECONDS));
    runnableToInvoke.get().run();
    verify(scheduledExecutor, times(1)).schedule(isA(Runnable.class), eq(interBatchDelay.toNanos()), eq(TimeUnit.NANOSECONDS));
    runnableToInvoke.get().run();
    verify(scheduledExecutor, times(1)).schedule(isA(Runnable.class), eq(1234L), eq(TimeUnit.NANOSECONDS));
    runnableToInvoke.get().run();
    verify(scheduledExecutor, times(3)).schedule(isA(Runnable.class), anyLong(), any());
}