From e2c2bee5d6c514f85fd8f880c062eada96b43cc0 Mon Sep 17 00:00:00 2001 From: Luca Venturi Date: Wed, 31 Jan 2024 13:36:07 +0100 Subject: [PATCH] Fixes tests and allows autohealing in fibers --- .../lucaventuri/examples/ChoiceExample.java | 2 +- .../lucaventuri/examples/HttpExampleMini.java | 2 -- .../eu/lucaventuri/fibry/ActorSystem.java | 2 -- .../java/eu/lucaventuri/fibry/ActorUtils.java | 4 +--- .../eu/lucaventuri/fibry/Stereotypes.java | 2 +- .../java/eu/lucaventuri/fibry/LoadTests.java | 19 +++++-------------- .../eu/lucaventuri/fibry/TestAutoHealing.java | 5 ----- 7 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/main/java/eu/lucaventuri/examples/ChoiceExample.java b/src/main/java/eu/lucaventuri/examples/ChoiceExample.java index f24fe8a..91690ab 100644 --- a/src/main/java/eu/lucaventuri/examples/ChoiceExample.java +++ b/src/main/java/eu/lucaventuri/examples/ChoiceExample.java @@ -6,7 +6,7 @@ public class ChoiceExample { public static void main(String[] args) { - ActorUtils.runAsFiberScope(() -> System.out.println("Naked fiber 1!"), () -> System.out.println("Naked fiber 2!"), () -> System.out.println("Naked fiber 3!")); + // ActorUtils.runAsFiberScope(() -> System.out.println("Naked fiber 1!"), () -> System.out.println("Naked fiber 2!"), () -> System.out.println("Naked fiber 3!")); ActorUtils.runAsFiber(() -> System.out.println("Naked fiber GS 1!"), () -> System.out.println("Naked fiber GS 2!"), () -> System.out.println("Naked fiber GS 3!")); diff --git a/src/main/java/eu/lucaventuri/examples/HttpExampleMini.java b/src/main/java/eu/lucaventuri/examples/HttpExampleMini.java index 329ee58..9410a9c 100644 --- a/src/main/java/eu/lucaventuri/examples/HttpExampleMini.java +++ b/src/main/java/eu/lucaventuri/examples/HttpExampleMini.java @@ -10,8 +10,6 @@ public class HttpExampleMini { public static void main(String[] args) throws IOException { int port = 18000; - System.out.println("Fibers V1 available: " + ActorUtils.areFibersV1Available()); - System.out.println("Fibers V2 available: " + ActorUtils.areFibersV2Available()); System.out.println("Fibers available: " + ActorUtils.areFibersAvailable()); Stereotypes.def().embeddedHttpServer(port, new Stereotypes.HttpStringWorker("/", ex -> "Hello world from Fibry!")); System.out.println("Listening on http://localhost:" + port); diff --git a/src/main/java/eu/lucaventuri/fibry/ActorSystem.java b/src/main/java/eu/lucaventuri/fibry/ActorSystem.java index c6be0db..b98341f 100644 --- a/src/main/java/eu/lucaventuri/fibry/ActorSystem.java +++ b/src/main/java/eu/lucaventuri/fibry/ActorSystem.java @@ -261,8 +261,6 @@ public NamedStateActorCreator pollTimeout(int newPollTimeoutMs) { } public NamedStateActorCreator autoHealing(AutoHealingSettings newAutoHealing) { - if (strategy == CreationStrategy.FIBER || (strategy == CreationStrategy.AUTO && ActorUtils.areFibersAvailable())) - throw new UnsupportedOperationException("AutoHealing for now is only supported on threads"); return new NamedStateActorCreator(name, strategy, initialState, allowReuse, initializer, finalizer, closeStrategy, queueCapacity, pollTimeoutMs, newAutoHealing); } diff --git a/src/main/java/eu/lucaventuri/fibry/ActorUtils.java b/src/main/java/eu/lucaventuri/fibry/ActorUtils.java index 025446e..83b232d 100644 --- a/src/main/java/eu/lucaventuri/fibry/ActorUtils.java +++ b/src/main/java/eu/lucaventuri/fibry/ActorUtils.java @@ -254,9 +254,7 @@ private static boolean isMethodHandler(Method method) { } public static ExecutorService newVirtualThreadsExecutor() { - try (ExecutorService virtualThreadPerTaskExecutor = Executors.newVirtualThreadPerTaskExecutor()) { - return virtualThreadPerTaskExecutor; - } + return Executors.newVirtualThreadPerTaskExecutor(); } /** diff --git a/src/main/java/eu/lucaventuri/fibry/Stereotypes.java b/src/main/java/eu/lucaventuri/fibry/Stereotypes.java index bf46673..3eb97db 100644 --- a/src/main/java/eu/lucaventuri/fibry/Stereotypes.java +++ b/src/main/java/eu/lucaventuri/fibry/Stereotypes.java @@ -295,7 +295,7 @@ public void embeddedHttpServer(int port, Function rootWork public HttpServer embeddedHttpServer(int port, HttpStringWorker... workers) throws IOException { //NamedStateActorCreator config = anonymous().initialState(null); HttpServer server = HttpServer.create(new InetSocketAddress(port), defaultHttpBacklog.get()); - server.setExecutor(AUTO.newExecutor()); + server.setExecutor(FIBER.newExecutor()); for (HttpStringWorker worker : workers) { server.createContext(worker.context, exchange -> { diff --git a/src/test/java/eu/lucaventuri/fibry/LoadTests.java b/src/test/java/eu/lucaventuri/fibry/LoadTests.java index 5669e84..a037e84 100644 --- a/src/test/java/eu/lucaventuri/fibry/LoadTests.java +++ b/src/test/java/eu/lucaventuri/fibry/LoadTests.java @@ -17,28 +17,19 @@ public class LoadTests { @Test public void testHttp() throws IOException, URISyntaxException, InterruptedException { - boolean fibersAvailable = ActorUtils.areFibersAvailable(); - - System.out.println("Fibers: " + fibersAvailable); - var num = new AtomicInteger(); int port = 10001; - var url = new URL("http://localhost:" + port + "/test"); var uri = new URI("http://localhost:" + port + "/test"); Stereotypes.def().embeddedHttpServer(port, new Stereotypes.HttpStringWorker("/test", ex -> ""+num.incrementAndGet())); - final int numThreads; - final int numCalls; + final int numThreads = 250; + final int numCalls = 100; - if (fibersAvailable) { - numThreads = 250; - numCalls = 100; - } else { - numThreads = 100; - numCalls = 100; - } + System.out.println(uri); + + //Thread.sleep(300_00); CountDownLatch latch = new CountDownLatch(numThreads); var client = HttpUtil.getHttpClient(10); diff --git a/src/test/java/eu/lucaventuri/fibry/TestAutoHealing.java b/src/test/java/eu/lucaventuri/fibry/TestAutoHealing.java index f285c2c..78a9c64 100644 --- a/src/test/java/eu/lucaventuri/fibry/TestAutoHealing.java +++ b/src/test/java/eu/lucaventuri/fibry/TestAutoHealing.java @@ -112,11 +112,6 @@ public void testMax() throws InterruptedException, ExecutionException { Assert.assertTrue(end - partial >= 3000); } - @Test(expected = UnsupportedOperationException.class) - public void testFailOnFibers() { - ActorSystem.anonymous().strategy(CreationStrategy.FIBER).autoHealing(new ActorSystem.AutoHealingSettings(1, 2, null, null)).newActor(SystemUtils::sleepEnsure); - } - @Test public void testMaxRecovering() throws InterruptedException, ExecutionException { HealRegistry.INSTANCE.setGracePeriod(10, TimeUnit.MILLISECONDS);