Add tests to PeriodicSyncTask
Signed-off-by: Quentin Legot <legotquentin@gmail.com>
This commit is contained in:
parent
e9c97df089
commit
9123d71a7e
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
||||
}
|
@ -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<Integer> 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());
|
||||
}
|
||||
});
|
@ -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<AtomicInteger> 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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user