TSK-1757: Added ParallelThreadHelper to support multi threaded tests.
This commit is contained in:
parent
315637c534
commit
1df190d1da
|
@ -0,0 +1,30 @@
|
|||
package pro.taskana.common.test.util;
|
||||
|
||||
import java.lang.Thread.UncaughtExceptionHandler;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import pro.taskana.common.api.exceptions.SystemException;
|
||||
|
||||
public class ParallelThreadHelper {
|
||||
|
||||
public static void runInThread(Runnable runnable, int threadCount) throws Exception {
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
|
||||
Collection<Throwable> errors = new ConcurrentLinkedQueue<>();
|
||||
UncaughtExceptionHandler uncaughtExceptionHandler = (t, e) -> errors.add(e);
|
||||
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i] = new Thread(runnable);
|
||||
threads[i].setUncaughtExceptionHandler(uncaughtExceptionHandler);
|
||||
threads[i].start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
errors.forEach(Throwable::printStackTrace);
|
||||
throw new SystemException("at least 1 thread caught an exception.");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,6 +22,7 @@ import pro.taskana.common.internal.JobServiceImpl;
|
|||
import pro.taskana.common.internal.jobs.JobRunner;
|
||||
import pro.taskana.common.internal.jobs.PlainJavaTransactionProvider;
|
||||
import pro.taskana.common.test.config.DataSourceGenerator;
|
||||
import pro.taskana.common.test.util.ParallelThreadHelper;
|
||||
import pro.taskana.task.internal.jobs.TaskCleanupJob;
|
||||
|
||||
@Disabled
|
||||
|
@ -37,7 +38,7 @@ class JobRunnerAccTest extends AbstractAccTest {
|
|||
ScheduledJob job = createJob(Instant.now().minus(5, ChronoUnit.MINUTES));
|
||||
assertThat(jobService.findJobsToRun()).containsExactly(job);
|
||||
|
||||
runInThread(
|
||||
ParallelThreadHelper.runInThread(
|
||||
() -> {
|
||||
try {
|
||||
TaskanaEngine taskanaEngine =
|
||||
|
@ -69,17 +70,6 @@ class JobRunnerAccTest extends AbstractAccTest {
|
|||
assertThat(jobsToRun).hasSize(1).doesNotContain(job);
|
||||
}
|
||||
|
||||
private void runInThread(Runnable runnable, int threadCount) throws Exception {
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i] = new Thread(runnable);
|
||||
threads[i].start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
private ScheduledJob createJob(Instant firstDue) {
|
||||
ScheduledJob job = new ScheduledJob();
|
||||
job.setType(TaskCleanupJob.class.getName());
|
||||
|
|
|
@ -22,6 +22,7 @@ import pro.taskana.common.api.security.UserPrincipal;
|
|||
import pro.taskana.common.internal.util.CheckedConsumer;
|
||||
import pro.taskana.common.test.security.JaasExtension;
|
||||
import pro.taskana.common.test.security.WithAccessId;
|
||||
import pro.taskana.common.test.util.ParallelThreadHelper;
|
||||
import pro.taskana.task.api.TaskQuery;
|
||||
import pro.taskana.task.api.TaskService;
|
||||
import pro.taskana.task.api.models.Task;
|
||||
|
@ -39,7 +40,8 @@ class SelectAndClaimTaskAccTest extends AbstractAccTest {
|
|||
Stream.of("admin", "teamlead-1", "teamlead-2", "taskadmin")
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
runInThread(getRunnableTest(selectedAndClaimedTasks, accessIds), accessIds.size());
|
||||
ParallelThreadHelper.runInThread(
|
||||
getRunnableTest(selectedAndClaimedTasks, accessIds), accessIds.size());
|
||||
|
||||
assertThat(selectedAndClaimedTasks)
|
||||
.extracting(Task::getId)
|
||||
|
@ -67,17 +69,6 @@ class SelectAndClaimTaskAccTest extends AbstractAccTest {
|
|||
+ "task query returned nothing!");
|
||||
}
|
||||
|
||||
private void runInThread(Runnable runnable, int threadCount) throws InterruptedException {
|
||||
Thread[] threads = new Thread[threadCount];
|
||||
for (int i = 0; i < threads.length; i++) {
|
||||
threads[i] = new Thread(runnable);
|
||||
threads[i].start();
|
||||
}
|
||||
for (Thread thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
private Runnable getRunnableTest(List<Task> selectedAndClaimedTasks, List<String> accessIds) {
|
||||
return () -> {
|
||||
Subject subject = new Subject();
|
||||
|
|
Loading…
Reference in New Issue