DockerTaskScheduler { void scheduleInstanceTask(@Nonnull DockerInstanceTask instanceTask) { DockerCloudUtils.requireNonNull(instanceTask, "Instance task cannot be null."); LOG.debug("Scheduling instance task: " + clientTasks); submitTaskWithInitialDelay(instanceTask); } DockerTaskScheduler(int threadPoolSize, boolean usingDaemonThread, Duration taskTimeout);   }
@Test public void differentInstanceTasksMayExecuteConcurrently() { scheduler = new DockerTaskScheduler(3, false, Duration.ofSeconds(2)); InstanceTestTaskDelegator task1 = new InstanceTestTaskDelegator(instance1); InstanceTestTaskDelegator task2 = new InstanceTestTaskDelegator(instance2); InstanceTestTaskDelegator task3 = new InstanceTestTaskDelegator(instance3); instanceLock.lock(); scheduler.scheduleInstanceTask(task1.task); scheduler.scheduleInstanceTask(task2.task); scheduler.scheduleInstanceTask(task3.task); waitMillis(500); assertThat(task1.isRunning()).isTrue(); assertThat(task2.isRunning()).isTrue(); assertThat(task3.isRunning()).isTrue(); }
@Test public void sameInstanceTasksMayNotExecuteConcurrently() { scheduler = new DockerTaskScheduler(3, false, Duration.ofSeconds(2)); InstanceTestTaskDelegator task1 = new InstanceTestTaskDelegator(instance1); InstanceTestTaskDelegator task2 = new InstanceTestTaskDelegator(instance2); InstanceTestTaskDelegator task2b = new InstanceTestTaskDelegator(instance2); instanceLock.lock(); scheduler.scheduleInstanceTask(task1.task); scheduler.scheduleInstanceTask(task2.task); scheduler.scheduleInstanceTask(task2b.task); waitMillis(500); assertThat(task1.isRunning()).isTrue(); assertThat(task2.isRunning()).isTrue(); assertThat(task2b.executions).isEmpty(); instanceLock.unlock(); waitMillis(500); assertThat(task2.isSuccessful()).isTrue(); assertThat(task2.isSuccessful()).isTrue(); assertThat(task2b.isSuccessful()).isTrue(); }
@Test public void noDaemonThreadByDefault() { scheduler = new DockerTaskScheduler(3, false, Duration.ofSeconds(2)); InstanceTestTaskDelegator task = new InstanceTestTaskDelegator(instance1); task.fixture = () -> assertThat(Thread.currentThread().isDaemon()).isFalse(); scheduler.scheduleInstanceTask(task.task); waitUntil(() -> task.executions.size() == 1 && task.executions.get(0).isSuccessful()); }
@Test public void daemonThreadsWhenFlagSet() { scheduler = new DockerTaskScheduler(3, true, Duration.ofSeconds(2)); InstanceTestTaskDelegator task = new InstanceTestTaskDelegator(instance1); task.fixture = () -> assertThat(Thread.currentThread().isDaemon()).isTrue(); scheduler.scheduleInstanceTask(task.task); waitUntil(task::isSuccessful); }
@Test public void interruptTaskAccordingToTimeout() { InstanceTestTaskDelegator task = new InstanceTestTaskDelegator(instance1); scheduler = new DockerTaskScheduler(3, false, Duration.ofMillis(500)); task.fixture = () -> { Stopwatch sw = Stopwatch.start(); try { Thread.sleep(TimeUnit.SECONDS.toMillis(2)); fail("Not interrupted."); } catch (InterruptedException e) { assertThat(sw.getDuration().toMillis()).isCloseTo(500, offset(100L)); } }; scheduler.scheduleInstanceTask(task.task); waitUntil(task::isSuccessful); }
@Test public void timeoutMustOnlyObserveExecutionTime() { InstanceTestTaskDelegator task1 = new InstanceTestTaskDelegator(instance1); InstanceTestTaskDelegator task2 = new InstanceTestTaskDelegator(instance1); scheduler = new DockerTaskScheduler(1, false, Duration.ofMillis(500)); task1.fixture = () -> waitSec(3); task2.fixture = () -> waitMillis(200); scheduler.scheduleInstanceTask(task1.task); scheduler.scheduleInstanceTask(task2.task); waitUntil(() -> task1.isFailed() && task2.isSuccessful()); }