fabric-synctask #1

Merged
quentinlegot merged 10 commits from fabric-synctask into master 2023-02-13 15:26:38 +01:00
5 changed files with 62 additions and 19 deletions
Showing only changes of commit 9123d71a7e - Show all commits

View File

@ -2,6 +2,6 @@ package fr.altarik.toolbox.task;
public interface TaskI { public interface TaskI {
public void addTask(AltarikRunnable function) throws InterruptedException; void addTask(AltarikRunnable function) throws InterruptedException;
} }

View File

@ -3,7 +3,6 @@ package fr.altarik.toolbox.task.sync;
import fr.altarik.toolbox.task.AltarikRunnable; import fr.altarik.toolbox.task.AltarikRunnable;
import fr.altarik.toolbox.task.PeriodicTaskI; import fr.altarik.toolbox.task.PeriodicTaskI;
import fr.altarik.toolbox.task.SchedulerTaskData; import fr.altarik.toolbox.task.SchedulerTaskData;
import fr.altarik.toolbox.task.TaskI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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(); return new PeriodicSyncTask();
} }
@Override @Override
public void addTask(AltarikRunnable function) throws InterruptedException { public void addTask(AltarikRunnable function) {
addTask(function, 0, 1); addTask(function, 0, 0);
} }
@Override @Override
@ -51,7 +50,7 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable {
} }
@Override @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)); tasks.add(new SchedulerTaskData(function, delay, period));
} }
} }

View File

@ -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.
}

View File

@ -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.AltarikRunnable;
import fr.altarik.toolbox.task.async.AsyncTasks;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Date; import java.util.Date;
@ -19,16 +18,16 @@ class AsyncTaskTest {
@Test @Test
void testAsyncOp() throws Exception { void testAsyncOp() throws Exception {
int numberOfTasks = 10000; 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 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<>(); Stack<Integer> results = new Stack<>();
for(int i = 0; i < numberOfTasks; i++) { 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); AtomicInteger atomicInteger = new AtomicInteger(i);
worker.addTask(new AltarikRunnable() { worker.addTask(new AltarikRunnable() {
@Override @Override
public void run() { public void run() {
System.out.println(log(" task " + atomicInteger.get())); // System.out.println(log(" task " + atomicInteger.get()));
results.push(atomicInteger.get()); results.push(atomicInteger.get());
} }
}); });

View File

@ -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());
}
}