@Override
  public void run() {
    while (!schedulerState.isShuttingDown()) {
      if (!schedulerState.isPaused()) {
        try {
          toRun.run();
        } catch (Throwable e) {
          LOG.error("Unhandled exception. Will keep running.", e);
          schedulerListeners.onSchedulerEvent(SchedulerEventType.UNEXPECTED_ERROR);
        }
      }

      try {
        waitBetweenRuns.doWait();
      } catch (InterruptedException interruptedException) {
        if (schedulerState.isShuttingDown()) {
          LOG.debug("Thread '{}' interrupted due to shutdown.", Thread.currentThread().getName());
        } else {
          LOG.error("Unexpected interruption of thread. Will keep running.", interruptedException);
          schedulerListeners.onSchedulerEvent(SchedulerEventType.UNEXPECTED_ERROR);
        }
      }
    }
  }
@Test
  public void should_wait_on_ok_execution() {
    Assertions.assertTimeoutPreemptively(
        Duration.ofSeconds(1),
        () -> {
          runUntilShutdown.run();
          assertThat(countingWaiter.counter, is(2));
        });
  }