Skip to content
Snippets Groups Projects
Verified Commit 3b85b5c7 authored by Luc Everse's avatar Luc Everse :passport_control:
Browse files

Fix the worker run test

parent c352b977
Branches
Tags
2 merge requests!27Version release 1.2.0,!18Fix the worker run test
Pipeline #155283 passed
......@@ -193,6 +193,17 @@ public class Threads {
return this.scheduledExecutor.scheduleWithFixedDelay(this.wrap(task), delay, delay, unit);
}
/**
* Submits a task as a separate, unmanaged and unpooled thread.
*
* @param runnable the task to execute
*
* @return the thread executing the task
*/
public Thread asThread(final UnsafeRunnable runnable) {
return new Thread(this.wrap(runnable));
}
/**
* Wraps an unsafe runnable into a runnable that will catch the exceptions it could throw.
*
......
package nl.tudelft.ewi.auta.worker;
import nl.tudelft.ewi.auta.common.threads.Threads;
import nl.tudelft.ewi.auta.worker.conn.MessageRelay;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
......@@ -84,12 +85,14 @@ public class Worker {
final var httpClient = HttpClients.createDefault();
this.resolver.add(HttpClient.class, httpClient);
final var threads = this.resolver.get(Threads.class);
final var checker = this.resolver.get(Checker.class);
final var loader = this.resolver.get(CheckerLoader.class);
loader.load(checker);
final var relay = this.resolver.get(MessageRelay.class);
final var relayThread = new Thread(relay);
final var relayThread = threads.asThread(relay::run);
relayThread.start();
// Since Java appears to kill everything when the shutdown hook exits
......
package nl.tudelft.ewi.auta.worker;
import nl.tudelft.ewi.auta.common.threads.Threads;
import nl.tudelft.ewi.auta.common.threads.UnsafeRunnable;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
......@@ -14,6 +16,15 @@ public class WorkerTest {
final var resolver = new DependencyResolver();
resolver.add(MessageRelay.class, relay);
final var threads = Mockito.mock(Threads.class);
Mockito.when(threads.asThread(Mockito.any())).then(invocation -> {
final var runnable = (UnsafeRunnable) invocation.getArgument(0);
runnable.run();
return Mockito.mock(Thread.class);
});
resolver.add(Threads.class, threads);
final var argparse = new ArgumentParser();
final var worker = new Worker(resolver, argparse);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment