+ }
+ class App {
+ + main(args : String[]) {static}
+ }
+}
+AbstractTask ..|> Task
+SimpleTask --|> AbstractTask
+SumTask --|> AbstractTask
+TimeTrigger ..|> Trigger
+IntervalTrigger ..|> Trigger
+ConditionTrigger ..|> Trigger
+FirstComeFirstServedScheduler ..|> TaskScheduler
+PriorityScheduler ..|> TaskScheduler
+RoundRobinScheduler ..|> TaskScheduler
+ShortestRemainingTimeFirstScheduler ..|> TaskScheduler
+ScheduledJob --> "-trigger" Trigger
+ScheduledJob ..> Task : creates
+TaskScheduler ..> Task : runs
+Simulator --> "-scheduler" TaskScheduler
+Simulator --> "-jobs" ScheduledJob
+App ..> Simulator
+@enduml
diff --git a/scheduler/pom.xml b/scheduler/pom.xml
new file mode 100644
index 000000000000..e9df4bc819dc
--- /dev/null
+++ b/scheduler/pom.xml
@@ -0,0 +1,70 @@
+
+
+
+ 4.0.0
+
+ com.iluwatar
+ java-design-patterns
+ 1.26.0-SNAPSHOT
+
+ scheduler
+
+
+ org.slf4j
+ slf4j-api
+
+
+ ch.qos.logback
+ logback-classic
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ test
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-assembly-plugin
+
+
+
+
+
+ com.iluwatar.scheduler.App
+
+
+
+
+
+
+
+
+
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/AbstractTask.java b/scheduler/src/main/java/com/iluwatar/scheduler/AbstractTask.java
new file mode 100644
index 000000000000..dd02e6a7d372
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/AbstractTask.java
@@ -0,0 +1,71 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import lombok.Getter;
+
+/**
+ * Base class that keeps the time accounting shared by every task. Subclasses only implement {@link
+ * #doWork(int)}, the actual work done in one time slice.
+ */
+@Getter
+public abstract class AbstractTask implements Task {
+ private final int id;
+ private final int totalExecutionTime;
+ private final int priority;
+ private int elapsedTime;
+
+ protected AbstractTask(int id, int totalExecutionTime, int priority) {
+ if (totalExecutionTime <= 0) {
+ throw new IllegalArgumentException("Total execution time must be positive");
+ }
+ this.id = id;
+ this.totalExecutionTime = totalExecutionTime;
+ this.priority = priority;
+ }
+
+ @Override
+ public int getRemainingTime() {
+ return totalExecutionTime - elapsedTime;
+ }
+
+ @Override
+ public boolean isComplete() {
+ return elapsedTime >= totalExecutionTime;
+ }
+
+ @Override
+ public void execute(int deltaTime) {
+ if (isComplete()) {
+ throw new IllegalStateException("Task " + id + " is already complete");
+ }
+ var units = Math.min(deltaTime, getRemainingTime());
+ doWork(units);
+ elapsedTime += units;
+ }
+
+ /** Perform the task's own work for the given number of time units. */
+ protected abstract void doWork(int units);
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/App.java b/scheduler/src/main/java/com/iluwatar/scheduler/App.java
new file mode 100644
index 000000000000..4574f921b178
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/App.java
@@ -0,0 +1,68 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * The Scheduler pattern separates what work is done ({@link Task}), when it becomes ready ({@link
+ * Trigger}) and in which order it runs ({@link TaskScheduler}). The {@link Simulator} is the
+ * execution context that ties them together.
+ *
+ * This example runs the same workload through every scheduling strategy. The workload uses all
+ * three trigger types: tasks arriving at fixed times, a report repeating on an interval, and a
+ * cleanup task that starts only after a backup completes.
+ */
+@Slf4j
+public class App {
+
+ /** Program entry point. */
+ public static void main(String[] args) {
+ List schedulers =
+ List.of(
+ new FirstComeFirstServedScheduler(),
+ new PriorityScheduler(),
+ new RoundRobinScheduler(),
+ new ShortestRemainingTimeFirstScheduler());
+ for (var scheduler : schedulers) {
+ var completedAt = new Simulator(scheduler, workload(), 1, 30).simulate();
+ LOGGER.info("{} completed tasks at: {}", scheduler.getClass().getSimpleName(), completedAt);
+ }
+ }
+
+ /** Build a fresh workload, since tasks keep their progress once run. */
+ static List workload() {
+ var backup = new SimpleTask(1, 5, 1);
+ var reportIds = new AtomicInteger(100);
+ return List.of(
+ ScheduledJob.at(0, backup),
+ ScheduledJob.at(1, new SumTask(2, 4, 3)),
+ new ScheduledJob(
+ new IntervalTrigger(2, 10), () -> new SimpleTask(reportIds.incrementAndGet(), 2, 2)),
+ new ScheduledJob(new ConditionTrigger(backup::isComplete), () -> new SimpleTask(3, 3)));
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/ConditionTrigger.java b/scheduler/src/main/java/com/iluwatar/scheduler/ConditionTrigger.java
new file mode 100644
index 000000000000..b06c3346984d
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/ConditionTrigger.java
@@ -0,0 +1,46 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.function.BooleanSupplier;
+
+/** Fires once, the first time a condition becomes true, such as another task completing. */
+public class ConditionTrigger implements Trigger {
+ private final BooleanSupplier condition;
+ private boolean fired;
+
+ public ConditionTrigger(BooleanSupplier condition) {
+ this.condition = condition;
+ }
+
+ @Override
+ public boolean fires(int time) {
+ if (fired || !condition.getAsBoolean()) {
+ return false;
+ }
+ fired = true;
+ return true;
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/FirstComeFirstServedScheduler.java b/scheduler/src/main/java/com/iluwatar/scheduler/FirstComeFirstServedScheduler.java
new file mode 100644
index 000000000000..ba7b22030b4e
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/FirstComeFirstServedScheduler.java
@@ -0,0 +1,50 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+/** Tasks run to completion in the order they arrive. */
+public class FirstComeFirstServedScheduler implements TaskScheduler {
+ private final Queue taskQueue = new ArrayDeque<>();
+
+ @Override
+ public void scheduleTask(Task task) {
+ taskQueue.add(task);
+ }
+
+ @Override
+ public void update(int deltaTime) {
+ var task = taskQueue.peek();
+ if (task == null) {
+ return;
+ }
+ task.execute(deltaTime);
+ if (task.isComplete()) {
+ taskQueue.remove();
+ }
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/IntervalTrigger.java b/scheduler/src/main/java/com/iluwatar/scheduler/IntervalTrigger.java
new file mode 100644
index 000000000000..9689e0877122
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/IntervalTrigger.java
@@ -0,0 +1,41 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+/** Fires at {@code start} and then every {@code period} time units, like a cron job. */
+public record IntervalTrigger(int start, int period) implements Trigger {
+
+ /** Validate the period so that the trigger cannot divide by zero. */
+ public IntervalTrigger {
+ if (period <= 0) {
+ throw new IllegalArgumentException("Period must be positive");
+ }
+ }
+
+ @Override
+ public boolean fires(int time) {
+ return time >= start && (time - start) % period == 0;
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/PriorityScheduler.java b/scheduler/src/main/java/com/iluwatar/scheduler/PriorityScheduler.java
new file mode 100644
index 000000000000..2b132b899360
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/PriorityScheduler.java
@@ -0,0 +1,56 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.Comparator;
+import java.util.PriorityQueue;
+import java.util.Queue;
+
+/**
+ * The task with the highest priority runs first; a newly arrived task with a higher priority
+ * preempts the running one. Ties go to the task created first.
+ */
+public class PriorityScheduler implements TaskScheduler {
+ private final Queue taskQueue =
+ new PriorityQueue<>(
+ Comparator.comparingInt(Task::getPriority).reversed().thenComparingInt(Task::getId));
+
+ @Override
+ public void scheduleTask(Task task) {
+ taskQueue.add(task);
+ }
+
+ @Override
+ public void update(int deltaTime) {
+ var task = taskQueue.peek();
+ if (task == null) {
+ return;
+ }
+ task.execute(deltaTime);
+ if (task.isComplete()) {
+ taskQueue.remove();
+ }
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/RoundRobinScheduler.java b/scheduler/src/main/java/com/iluwatar/scheduler/RoundRobinScheduler.java
new file mode 100644
index 000000000000..b41dbda2031b
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/RoundRobinScheduler.java
@@ -0,0 +1,52 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+/**
+ * Tasks run in a cyclic order, each getting one time quantum before going to the back of the queue.
+ */
+public class RoundRobinScheduler implements TaskScheduler {
+ private final Queue taskQueue = new ArrayDeque<>();
+
+ @Override
+ public void scheduleTask(Task task) {
+ taskQueue.add(task);
+ }
+
+ @Override
+ public void update(int deltaTime) {
+ var task = taskQueue.poll();
+ if (task == null) {
+ return;
+ }
+ task.execute(deltaTime);
+ if (!task.isComplete()) {
+ taskQueue.add(task);
+ }
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/ScheduledJob.java b/scheduler/src/main/java/com/iluwatar/scheduler/ScheduledJob.java
new file mode 100644
index 000000000000..b5f42c28185d
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/ScheduledJob.java
@@ -0,0 +1,39 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.function.Supplier;
+
+/**
+ * Pairs a trigger with the task it creates. A factory is used because a repeating trigger needs a
+ * fresh task each time it fires.
+ */
+public record ScheduledJob(Trigger trigger, Supplier taskFactory) {
+
+ /** A job that hands the given task to the scheduler once, at the given time. */
+ public static ScheduledJob at(int time, Task task) {
+ return new ScheduledJob(new TimeTrigger(time), () -> task);
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstScheduler.java b/scheduler/src/main/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstScheduler.java
new file mode 100644
index 000000000000..d8cc48ab0709
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstScheduler.java
@@ -0,0 +1,57 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.Comparator;
+import java.util.PriorityQueue;
+import java.util.Queue;
+
+/**
+ * The task with the shortest remaining time runs next, so short tasks preempt long ones. Ties go to
+ * the task created first.
+ */
+public class ShortestRemainingTimeFirstScheduler implements TaskScheduler {
+ private final Queue taskQueue =
+ new PriorityQueue<>(
+ Comparator.comparingInt(Task::getRemainingTime).thenComparingInt(Task::getId));
+
+ @Override
+ public void scheduleTask(Task task) {
+ taskQueue.add(task);
+ }
+
+ @Override
+ public void update(int deltaTime) {
+ // Poll and re-add: the remaining time is the queue key and changes on every run.
+ var task = taskQueue.poll();
+ if (task == null) {
+ return;
+ }
+ task.execute(deltaTime);
+ if (!task.isComplete()) {
+ taskQueue.add(task);
+ }
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/SimpleTask.java b/scheduler/src/main/java/com/iluwatar/scheduler/SimpleTask.java
new file mode 100644
index 000000000000..e0d9d1bfe2eb
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/SimpleTask.java
@@ -0,0 +1,46 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import lombok.extern.slf4j.Slf4j;
+
+/** A task whose work is only to report its progress, such as a placeholder or a timed job. */
+@Slf4j
+public class SimpleTask extends AbstractTask {
+
+ public SimpleTask(int id, int totalExecutionTime) {
+ this(id, totalExecutionTime, 0);
+ }
+
+ public SimpleTask(int id, int totalExecutionTime, int priority) {
+ super(id, totalExecutionTime, priority);
+ }
+
+ @Override
+ protected void doWork(int units) {
+ LOGGER.info(
+ "Task {} is {}/{} complete", getId(), getElapsedTime() + units, getTotalExecutionTime());
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java b/scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java
new file mode 100644
index 000000000000..ed40a3c97da0
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java
@@ -0,0 +1,91 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The execution context: advances a clock, fires the triggers of each job, hands the created tasks
+ * to the scheduler and records when each task completes.
+ */
+public class Simulator {
+ private final TaskScheduler scheduler;
+ private final List jobs;
+ private final int deltaTime;
+ private final int simulateTime;
+
+ /**
+ * Create a simulator.
+ *
+ * @param scheduler the scheduling strategy under test
+ * @param jobs the jobs whose triggers create tasks; jobs firing at the same time are scheduled in
+ * list order
+ * @param deltaTime time units the scheduler runs per step
+ * @param simulateTime total time units to simulate
+ */
+ public Simulator(
+ TaskScheduler scheduler, List jobs, int deltaTime, int simulateTime) {
+ if (deltaTime <= 0) {
+ throw new IllegalArgumentException("Delta time must be positive");
+ }
+ this.scheduler = scheduler;
+ this.jobs = List.copyOf(jobs);
+ this.deltaTime = deltaTime;
+ this.simulateTime = simulateTime;
+ }
+
+ /**
+ * Run the simulation.
+ *
+ * @return task ids in the order they completed, each mapped to the time it completed
+ */
+ public Map simulate() {
+ Map completedAt = new LinkedHashMap<>();
+ List running = new ArrayList<>();
+ for (var time = 0; time < simulateTime; time += deltaTime) {
+ for (var job : jobs) {
+ if (job.trigger().fires(time)) {
+ var task = job.taskFactory().get();
+ running.add(task);
+ scheduler.scheduleTask(task);
+ }
+ }
+ scheduler.update(deltaTime);
+ var finishedAt = time + deltaTime;
+ running.removeIf(
+ task -> {
+ if (task.isComplete()) {
+ completedAt.put(task.getId(), finishedAt);
+ return true;
+ }
+ return false;
+ });
+ }
+ return completedAt;
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/SumTask.java b/scheduler/src/main/java/com/iluwatar/scheduler/SumTask.java
new file mode 100644
index 000000000000..d2ef5267ba27
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/SumTask.java
@@ -0,0 +1,54 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * A task that computes the sum of 1..n, adding one number per time unit, so the work can be paused
+ * and resumed between time slices.
+ */
+@Slf4j
+public class SumTask extends AbstractTask {
+ @Getter private long sum;
+ private int next = 1;
+
+ public SumTask(int id, int n) {
+ this(id, n, 0);
+ }
+
+ public SumTask(int id, int n, int priority) {
+ super(id, n, priority);
+ }
+
+ @Override
+ protected void doWork(int units) {
+ for (var i = 0; i < units; i++) {
+ sum += next++;
+ }
+ LOGGER.info("Task {} summed up to {}: {}", getId(), next - 1, sum);
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/Task.java b/scheduler/src/main/java/com/iluwatar/scheduler/Task.java
new file mode 100644
index 000000000000..dec23a57ce2d
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/Task.java
@@ -0,0 +1,51 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+/**
+ * A unit of work the scheduler can run in slices of time. Implementations decide what the work is;
+ * the scheduler only needs to know how much time is left and whether the task is done.
+ */
+public interface Task {
+
+ /** Unique id of the task. A lower id means the task was created earlier. */
+ int getId();
+
+ /** Priority of the task. The higher the number, the higher the priority. */
+ int getPriority();
+
+ /** Time units the task still needs before it is complete. */
+ int getRemainingTime();
+
+ /** Whether the task has finished all of its work. */
+ boolean isComplete();
+
+ /**
+ * Run the task for the given number of time units.
+ *
+ * @throws IllegalStateException if the task is already complete
+ */
+ void execute(int deltaTime);
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/TaskScheduler.java b/scheduler/src/main/java/com/iluwatar/scheduler/TaskScheduler.java
new file mode 100644
index 000000000000..9bc8b6c41b9f
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/TaskScheduler.java
@@ -0,0 +1,35 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+/** The scheduler decides which task runs next and for how long. */
+public interface TaskScheduler {
+
+ /** Add a task to the scheduler. */
+ void scheduleTask(Task task);
+
+ /** Run the scheduled tasks for the given number of time units. */
+ void update(int deltaTime);
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/TimeTrigger.java b/scheduler/src/main/java/com/iluwatar/scheduler/TimeTrigger.java
new file mode 100644
index 000000000000..8c59f2083305
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/TimeTrigger.java
@@ -0,0 +1,34 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+/** Fires once, at a fixed point in time. */
+public record TimeTrigger(int time) implements Trigger {
+
+ @Override
+ public boolean fires(int currentTime) {
+ return currentTime == time;
+ }
+}
diff --git a/scheduler/src/main/java/com/iluwatar/scheduler/Trigger.java b/scheduler/src/main/java/com/iluwatar/scheduler/Trigger.java
new file mode 100644
index 000000000000..a3dfd1c12676
--- /dev/null
+++ b/scheduler/src/main/java/com/iluwatar/scheduler/Trigger.java
@@ -0,0 +1,32 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+/** Decides when a job should hand a new task to the scheduler. */
+public interface Trigger {
+
+ /** Whether the trigger fires at the given simulation time. */
+ boolean fires(int time);
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/AppTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/AppTest.java
new file mode 100644
index 000000000000..0b9b595afaf7
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/AppTest.java
@@ -0,0 +1,46 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class AppTest {
+
+ @Test
+ void shouldExecuteApplicationWithoutException() {
+ assertDoesNotThrow(() -> App.main(new String[] {}));
+ }
+
+ @Test
+ void workloadRunsEveryTriggerType() {
+ var completedAt =
+ new Simulator(new FirstComeFirstServedScheduler(), App.workload(), 1, 30).simulate();
+ // backup, sum, three interval reports and the cleanup that waits for the backup
+ assertEquals(6, completedAt.size());
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/FirstComeFirstServedSchedulerTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/FirstComeFirstServedSchedulerTest.java
new file mode 100644
index 000000000000..c31c7f3639d9
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/FirstComeFirstServedSchedulerTest.java
@@ -0,0 +1,75 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+class FirstComeFirstServedSchedulerTest {
+ @Test
+ void testExecuteTasksFromBeginning() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(2, 2)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(3, 4)));
+
+ Simulator simulator = new Simulator(new FirstComeFirstServedScheduler(), jobs, 1, 100);
+
+ Map taskCompletedOrder = simulator.simulate();
+
+ assertEquals(3, taskCompletedOrder.size());
+ assertIterableEquals(List.of(1, 2, 3), taskCompletedOrder.keySet());
+ assertEquals(3, taskCompletedOrder.get(1));
+ assertEquals(5, taskCompletedOrder.get(2));
+ assertEquals(9, taskCompletedOrder.get(3));
+ }
+
+ @Test
+ void testExecuteTasks() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3)));
+ jobs.add(ScheduledJob.at(1, new SimpleTask(2, 2)));
+ jobs.add(ScheduledJob.at(6, new SimpleTask(3, 4)));
+ jobs.add(ScheduledJob.at(7, new SimpleTask(4, 2)));
+ jobs.add(ScheduledJob.at(8, new SimpleTask(5, 1)));
+
+ Simulator simulator = new Simulator(new FirstComeFirstServedScheduler(), jobs, 1, 100);
+
+ Map taskCompletedOrder = simulator.simulate();
+
+ assertEquals(5, taskCompletedOrder.size());
+ assertIterableEquals(List.of(1, 2, 3, 4, 5), taskCompletedOrder.keySet());
+ assertEquals(3, taskCompletedOrder.get(1));
+ assertEquals(5, taskCompletedOrder.get(2));
+ assertEquals(10, taskCompletedOrder.get(3));
+ assertEquals(12, taskCompletedOrder.get(4));
+ assertEquals(13, taskCompletedOrder.get(5));
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/PrioritySchedulerTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/PrioritySchedulerTest.java
new file mode 100644
index 000000000000..2f16204c7e62
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/PrioritySchedulerTest.java
@@ -0,0 +1,75 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+class PrioritySchedulerTest {
+ @Test
+ void testExecuteTasksFromBeginning() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3, 0)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(2, 2, 2)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(3, 1, 1)));
+
+ Simulator simulator = new Simulator(new PriorityScheduler(), jobs, 1, 10);
+
+ Map taskCompletedOrder = simulator.simulate();
+
+ assertEquals(3, taskCompletedOrder.size());
+ assertIterableEquals(List.of(2, 3, 1), taskCompletedOrder.keySet());
+ assertEquals(2, taskCompletedOrder.get(2));
+ assertEquals(3, taskCompletedOrder.get(3));
+ assertEquals(6, taskCompletedOrder.get(1));
+ }
+
+ @Test
+ void testExecuteTasks() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3, 1)));
+ jobs.add(ScheduledJob.at(1, new SimpleTask(2, 2, 0)));
+ jobs.add(ScheduledJob.at(2, new SimpleTask(3, 4, 5)));
+ jobs.add(ScheduledJob.at(7, new SimpleTask(4, 2, 4)));
+ jobs.add(ScheduledJob.at(8, new SimpleTask(5, 1, 2)));
+
+ Simulator simulator = new Simulator(new PriorityScheduler(), jobs, 1, 100);
+
+ Map taskCompletedOrder = simulator.simulate();
+
+ assertEquals(5, taskCompletedOrder.size());
+ assertIterableEquals(List.of(3, 1, 4, 5, 2), taskCompletedOrder.keySet());
+ assertEquals(6, taskCompletedOrder.get(3));
+ assertEquals(7, taskCompletedOrder.get(1));
+ assertEquals(9, taskCompletedOrder.get(4));
+ assertEquals(10, taskCompletedOrder.get(5));
+ assertEquals(12, taskCompletedOrder.get(2));
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/RoundRobinSchedulerTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/RoundRobinSchedulerTest.java
new file mode 100644
index 000000000000..bd665d4ab675
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/RoundRobinSchedulerTest.java
@@ -0,0 +1,74 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+class RoundRobinSchedulerTest {
+ @Test
+ void testExecuteTasksFromBeginning() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(2, 2)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(3, 4)));
+
+ Simulator simulator = new Simulator(new RoundRobinScheduler(), jobs, 1, 10);
+
+ Map taskCompletedOrder = simulator.simulate();
+ assertEquals(3, taskCompletedOrder.size());
+ assertIterableEquals(List.of(2, 1, 3), taskCompletedOrder.keySet());
+ assertEquals(5, taskCompletedOrder.get(2));
+ assertEquals(7, taskCompletedOrder.get(1));
+ assertEquals(9, taskCompletedOrder.get(3));
+ }
+
+ @Test
+ void testExecuteTasks() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3)));
+ jobs.add(ScheduledJob.at(1, new SimpleTask(2, 2)));
+ jobs.add(ScheduledJob.at(2, new SimpleTask(3, 4)));
+ jobs.add(ScheduledJob.at(7, new SimpleTask(4, 2)));
+ jobs.add(ScheduledJob.at(8, new SimpleTask(5, 1)));
+
+ Simulator simulator = new Simulator(new RoundRobinScheduler(), jobs, 1, 100);
+
+ Map taskCompletedOrder = simulator.simulate();
+
+ assertEquals(5, taskCompletedOrder.size());
+ assertIterableEquals(List.of(1, 2, 3, 5, 4), taskCompletedOrder.keySet());
+ assertEquals(4, taskCompletedOrder.get(1));
+ assertEquals(6, taskCompletedOrder.get(2));
+ assertEquals(10, taskCompletedOrder.get(3));
+ assertEquals(11, taskCompletedOrder.get(5));
+ assertEquals(12, taskCompletedOrder.get(4));
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstSchedulerTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstSchedulerTest.java
new file mode 100644
index 000000000000..408d6c239961
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstSchedulerTest.java
@@ -0,0 +1,71 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+class ShortestRemainingTimeFirstSchedulerTest {
+ @Test
+ void testExecuteTasksFromBeginning() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(2, 2)));
+ jobs.add(ScheduledJob.at(0, new SimpleTask(3, 4)));
+
+ Simulator simulator = new Simulator(new ShortestRemainingTimeFirstScheduler(), jobs, 1, 10);
+
+ Map taskCompletedOrder = simulator.simulate();
+ assertEquals(3, taskCompletedOrder.size());
+ assertIterableEquals(List.of(2, 1, 3), taskCompletedOrder.keySet());
+ }
+
+ @Test
+ void testExecuteTasks() {
+ List jobs = new ArrayList<>();
+ jobs.add(ScheduledJob.at(0, new SimpleTask(1, 3)));
+ jobs.add(ScheduledJob.at(1, new SimpleTask(2, 2)));
+ jobs.add(ScheduledJob.at(2, new SimpleTask(3, 4)));
+ jobs.add(ScheduledJob.at(7, new SimpleTask(4, 2)));
+ jobs.add(ScheduledJob.at(8, new SimpleTask(5, 1)));
+
+ Simulator simulator = new Simulator(new ShortestRemainingTimeFirstScheduler(), jobs, 1, 100);
+
+ Map taskCompletedOrder = simulator.simulate();
+
+ assertEquals(5, taskCompletedOrder.size());
+ assertIterableEquals(List.of(1, 2, 3, 5, 4), taskCompletedOrder.keySet());
+ assertEquals(3, taskCompletedOrder.get(1));
+ assertEquals(5, taskCompletedOrder.get(2));
+ assertEquals(9, taskCompletedOrder.get(3));
+ assertEquals(10, taskCompletedOrder.get(5));
+ assertEquals(12, taskCompletedOrder.get(4));
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/SimulatorTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/SimulatorTest.java
new file mode 100644
index 000000000000..1e399bb7cb60
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/SimulatorTest.java
@@ -0,0 +1,70 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.jupiter.api.Test;
+
+class SimulatorTest {
+
+ @Test
+ void intervalTriggerSchedulesFreshTaskEachPeriod() {
+ var ids = new AtomicInteger();
+ var job =
+ new ScheduledJob(new IntervalTrigger(0, 5), () -> new SimpleTask(ids.incrementAndGet(), 1));
+
+ var completedAt =
+ new Simulator(new FirstComeFirstServedScheduler(), List.of(job), 1, 15).simulate();
+
+ assertEquals(Map.of(1, 1, 2, 6, 3, 11), completedAt);
+ }
+
+ @Test
+ void conditionTriggerStartsTaskAfterAnotherCompletes() {
+ var first = new SimpleTask(1, 2);
+ var jobs =
+ List.of(
+ ScheduledJob.at(0, first),
+ new ScheduledJob(new ConditionTrigger(first::isComplete), () -> new SimpleTask(2, 1)));
+
+ var completedAt = new Simulator(new FirstComeFirstServedScheduler(), jobs, 1, 10).simulate();
+
+ assertEquals(List.of(1, 2), List.copyOf(completedAt.keySet()));
+ assertEquals(2, completedAt.get(1));
+ assertEquals(3, completedAt.get(2));
+ }
+
+ @Test
+ void simulatorRejectsNonPositiveDeltaTime() {
+ var scheduler = new FirstComeFirstServedScheduler();
+ List jobs = List.of();
+ assertThrows(IllegalArgumentException.class, () -> new Simulator(scheduler, jobs, 0, 10));
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/TaskTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/TaskTest.java
new file mode 100644
index 000000000000..1a7d8478512f
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/TaskTest.java
@@ -0,0 +1,69 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+class TaskTest {
+
+ @Test
+ void taskCompletesAfterItsExecutionTime() {
+ var task = new SimpleTask(1, 3, 2);
+ task.execute(2);
+ assertFalse(task.isComplete());
+ assertEquals(1, task.getRemainingTime());
+ task.execute(1);
+ assertTrue(task.isComplete());
+ assertEquals(0, task.getRemainingTime());
+ assertEquals(2, task.getPriority());
+ }
+
+ @Test
+ void executingCompletedTaskThrows() {
+ var task = new SimpleTask(1, 1);
+ task.execute(1);
+ assertThrows(IllegalStateException.class, () -> task.execute(1));
+ }
+
+ @Test
+ void taskRejectsNonPositiveExecutionTime() {
+ assertThrows(IllegalArgumentException.class, () -> new SimpleTask(1, 0));
+ }
+
+ @Test
+ void sumTaskResumesWorkAcrossSlices() {
+ var task = new SumTask(1, 10);
+ task.execute(4);
+ assertEquals(10, task.getSum());
+ task.execute(100);
+ assertEquals(55, task.getSum());
+ assertTrue(task.isComplete());
+ }
+}
diff --git a/scheduler/src/test/java/com/iluwatar/scheduler/TriggerTest.java b/scheduler/src/test/java/com/iluwatar/scheduler/TriggerTest.java
new file mode 100644
index 000000000000..232d3e7a2470
--- /dev/null
+++ b/scheduler/src/test/java/com/iluwatar/scheduler/TriggerTest.java
@@ -0,0 +1,68 @@
+/*
+ * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
+ *
+ * The MIT License
+ * Copyright © 2014-2022 Ilkka Seppälä
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.iluwatar.scheduler;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.junit.jupiter.api.Test;
+
+class TriggerTest {
+
+ @Test
+ void timeTriggerFiresOnlyAtItsTime() {
+ var trigger = new TimeTrigger(3);
+ assertFalse(trigger.fires(2));
+ assertTrue(trigger.fires(3));
+ assertFalse(trigger.fires(4));
+ }
+
+ @Test
+ void intervalTriggerFiresEveryPeriodFromStart() {
+ var trigger = new IntervalTrigger(2, 5);
+ assertFalse(trigger.fires(0));
+ assertTrue(trigger.fires(2));
+ assertFalse(trigger.fires(5));
+ assertTrue(trigger.fires(7));
+ assertTrue(trigger.fires(12));
+ }
+
+ @Test
+ void intervalTriggerRejectsNonPositivePeriod() {
+ assertThrows(IllegalArgumentException.class, () -> new IntervalTrigger(0, 0));
+ }
+
+ @Test
+ void conditionTriggerFiresOnceWhenConditionHolds() {
+ var condition = new AtomicBoolean(false);
+ var trigger = new ConditionTrigger(condition::get);
+ assertFalse(trigger.fires(0));
+ condition.set(true);
+ assertTrue(trigger.fires(1));
+ assertFalse(trigger.fires(2));
+ }
+}