From 9123d71a7e04dc491b6e18f1b76784e5772ff23d Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 12 Feb 2023 14:24:09 +0100 Subject: [PATCH] Add tests to PeriodicSyncTask Signed-off-by: Quentin Legot --- .../java/fr/altarik/toolbox/task/TaskI.java | 2 +- .../toolbox/task/sync/PeriodicSyncTask.java | 9 ++-- .../fr/altarik/toolbox/task/SyncTaskTest.java | 7 --- .../task/{ => async}/AsyncTaskTest.java | 11 ++-- .../task/sync/PeriodicSyncTaskTest.java | 52 +++++++++++++++++++ 5 files changed, 62 insertions(+), 19 deletions(-) delete mode 100644 Tasks/src/test/java/fr/altarik/toolbox/task/SyncTaskTest.java rename Tasks/src/test/java/fr/altarik/toolbox/task/{ => async}/AsyncTaskTest.java (78%) create mode 100644 Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java index a98753c..2f668a8 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java @@ -2,6 +2,6 @@ package fr.altarik.toolbox.task; public interface TaskI { - public void addTask(AltarikRunnable function) throws InterruptedException; + void addTask(AltarikRunnable function) throws InterruptedException; } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java index c5cee4e..c4e38f9 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/sync/PeriodicSyncTask.java @@ -3,7 +3,6 @@ package fr.altarik.toolbox.task.sync; import fr.altarik.toolbox.task.AltarikRunnable; import fr.altarik.toolbox.task.PeriodicTaskI; import fr.altarik.toolbox.task.SchedulerTaskData; -import fr.altarik.toolbox.task.TaskI; import java.util.ArrayList; import java.util.List; @@ -19,13 +18,13 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable { } - public static TaskI initialize() { + public static PeriodicTaskI initialize() { return new PeriodicSyncTask(); } @Override - public void addTask(AltarikRunnable function) throws InterruptedException { - addTask(function, 0, 1); + public void addTask(AltarikRunnable function) { + addTask(function, 0, 0); } @Override @@ -51,7 +50,7 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable { } @Override - public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException { + public void addTask(AltarikRunnable function, long delay, long period) { tasks.add(new SchedulerTaskData(function, delay, period)); } } diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/SyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/SyncTaskTest.java deleted file mode 100644 index 7095e58..0000000 --- a/Tasks/src/test/java/fr/altarik/toolbox/task/SyncTaskTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package fr.altarik.toolbox.task; - -public class SyncTaskTest { - - // TODO: 03/02/2023 Envoyé les tasks au workers grâce à un autre thread. - -} diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/AsyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/async/AsyncTaskTest.java similarity index 78% rename from Tasks/src/test/java/fr/altarik/toolbox/task/AsyncTaskTest.java rename to Tasks/src/test/java/fr/altarik/toolbox/task/async/AsyncTaskTest.java index c73d2a7..7e2f876 100644 --- a/Tasks/src/test/java/fr/altarik/toolbox/task/AsyncTaskTest.java +++ b/Tasks/src/test/java/fr/altarik/toolbox/task/async/AsyncTaskTest.java @@ -1,7 +1,6 @@ -package fr.altarik.toolbox.task; +package fr.altarik.toolbox.task.async; -import fr.altarik.toolbox.task.async.AsyncTaskI; -import fr.altarik.toolbox.task.async.AsyncTasks; +import fr.altarik.toolbox.task.AltarikRunnable; import org.junit.jupiter.api.Test; import java.util.Date; @@ -19,16 +18,16 @@ class AsyncTaskTest { @Test void testAsyncOp() throws Exception { int numberOfTasks = 10000; - System.out.println("Initializing async tasks worker"); + // System.out.println("Initializing async tasks worker"); AsyncTaskI worker = AsyncTasks.initialize(1); // only testing on a single worker, otherwise result have a high chance to not be in the order we want Stack results = new Stack<>(); for(int i = 0; i < numberOfTasks; i++) { - System.out.println(log("sending task " + i)); + // System.out.println(log("sending task " + i)); AtomicInteger atomicInteger = new AtomicInteger(i); worker.addTask(new AltarikRunnable() { @Override public void run() { - System.out.println(log(" task " + atomicInteger.get())); + // System.out.println(log(" task " + atomicInteger.get())); results.push(atomicInteger.get()); } }); diff --git a/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java new file mode 100644 index 0000000..b5d0a90 --- /dev/null +++ b/Tasks/src/test/java/fr/altarik/toolbox/task/sync/PeriodicSyncTaskTest.java @@ -0,0 +1,52 @@ +package fr.altarik.toolbox.task.sync; + +import fr.altarik.toolbox.task.AltarikRunnable; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class PeriodicSyncTaskTest { + + @Test + void testPeriodicSyncTask() { + List results = new ArrayList<>(); + PeriodicSyncTask worker = (PeriodicSyncTask) PeriodicSyncTask.initialize(); + AtomicInteger value1 = new AtomicInteger(1); + AtomicInteger value2 = new AtomicInteger(2); + worker.addTask(new AltarikRunnable() { + + @Override + public void run() { + results.add(value1); + } + }, 1, 2); + worker.addTask(new AltarikRunnable() { + private int i = 0; + @Override + public void run() { + results.add(value2); + if(i++ == 5) + this.cancel(); + } + }); + for(int i = 0; i < 10; i++) { + worker.run(); + } + AtomicInteger[] expected = { + value2, + value1, + value2, + value2, + value2, + value1, + value2, + value2, + value1 + }; + Assertions.assertArrayEquals(expected, results.toArray()); + } + +}