DockerTaskScheduler { void scheduleClientTask(@Nonnull DockerClientTask clientTask) { DockerCloudUtils.requireNonNull(clientTask, "Client task cannot be null."); LOG.debug("Scheduling client: " + clientTask); submitTaskWithInitialDelay(clientTask); } DockerTaskScheduler(int threadPoolSize, boolean usingDaemonThread, Duration taskTimeout);   }
@Test public void multipleClientTaskAreExecutedSynchronously() { scheduler = new DockerTaskScheduler(3, false, Duration.ofSeconds(2)); ClientTestTaskDelegator clientTask1 = new ClientTestTaskDelegator(); ClientTestTaskDelegator clientTask2 = new ClientTestTaskDelegator(); clientLock.lock(); scheduler.scheduleClientTask(clientTask1.task); scheduler.scheduleClientTask(clientTask2.task); waitMillis(500); assertThat(clientTask1.executions).hasSize(1).first().matches(Execution::isRunning); assertThat(clientTask2.executions).isEmpty(); clientLock.unlock(); waitMillis(500); assertThat(clientTask2.isSuccessful()).isTrue(); }
@Test public void initialDelay() { ClientTestTaskDelegator taskWithoutInitialDelay = new ClientTestTaskDelegator(); scheduler = new DockerTaskScheduler(3, false, Duration.ofSeconds(1)); scheduler.scheduleClientTask(taskWithoutInitialDelay.task); waitMillis(200); assertThat(taskWithoutInitialDelay.isSuccessful()).isTrue(); ClientTestTaskDelegator taskWithInitialDelay = new ClientTestTaskDelegator(Duration.ofSeconds(2), Duration.ofMillis(-1)); scheduler.scheduleClientTask(taskWithInitialDelay.task); assertThat(Stopwatch.measure(() -> waitUntil(taskWithInitialDelay::isSuccessful)).toMillis()). isCloseTo(2000, offset(150L)); }
@Test public void failedRepeatableTaskMustBeRescheduled() { ClientTestTaskDelegator failingRepeatableTask = new ClientTestTaskDelegator(Duration.ZERO, Duration .ofMillis(400)); failingRepeatableTask.fixture = () -> { throw new RuntimeException("Simulated failure"); }; scheduler = new DockerTaskScheduler(3, false, Duration.ofSeconds(1)); scheduler.scheduleClientTask(failingRepeatableTask.task); waitSec(1); assertThat(failingRepeatableTask.executions).hasSize(3); failingRepeatableTask.executions. forEach(execution -> assertThat(execution).matches(Execution::isFailed)); }