From cd4254b9daeeb3d95472eed2efe00de74859e665 Mon Sep 17 00:00:00 2001 From: Danilo Reinert Date: Sat, 29 Apr 2023 12:53:37 -0300 Subject: [PATCH] #147 [vrtx] Introduce VertxAsyncRunner --- .../requestor/vertx/VertxAsyncRunner.java | 98 +++++++++++++++++++ .../requestor/vertx/VertxAsyncRunnerTest.java | 84 ++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 requestor/ext/requestor-vertx/src/main/java/io/reinert/requestor/vertx/VertxAsyncRunner.java create mode 100644 requestor/ext/requestor-vertx/src/test/java/io/reinert/requestor/vertx/VertxAsyncRunnerTest.java diff --git a/requestor/ext/requestor-vertx/src/main/java/io/reinert/requestor/vertx/VertxAsyncRunner.java b/requestor/ext/requestor-vertx/src/main/java/io/reinert/requestor/vertx/VertxAsyncRunner.java new file mode 100644 index 000000000..318e3f41e --- /dev/null +++ b/requestor/ext/requestor-vertx/src/main/java/io/reinert/requestor/vertx/VertxAsyncRunner.java @@ -0,0 +1,98 @@ +/* + * Copyright 2023 Danilo Reinert + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.reinert.requestor.vertx; + +import io.reinert.requestor.core.AsyncRunner; +import io.vertx.core.AsyncResult; +import io.vertx.core.Vertx; + +/** + * AsyncRunner that integrates Requestor with Vertx. + * + * @author Danilo Reinert + */ +public class VertxAsyncRunner implements AsyncRunner { + + public VertxAsyncRunner(Vertx vertx) { + this.vertx = vertx; + } + private final Vertx vertx; + + @Override + public void run(Runnable runnable, long delayMillis) { + if (delayMillis < 1) { + executeBlocking(runnable); + } else { + vertx.setTimer(delayMillis, ignored -> executeBlocking(runnable)); + } + } + + @Override + public void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + private void executeBlocking(Runnable runnable) { + vertx.executeBlocking(promise -> { + runnable.run(); + promise.complete(); + }, VertxAsyncRunner::ignore); + } + + private static void ignore(AsyncResult res) { + // no-op + } + + @Override + public void shutdown() { + // no-op + } + + @Override + public boolean isShutdown() { + return false; + } + + @Override + public Lock getLock() { + return new Lock(); + } + + public static class Lock implements AsyncRunner.Lock { + + private volatile boolean awaiting = false; + + @Override + public synchronized void await(long timeout) throws InterruptedException { + awaiting = true; + wait(timeout); + } + + @Override + public boolean isAwaiting() { + return awaiting; + } + + @Override + public synchronized void signalAll() { + notifyAll(); + } + } +} diff --git a/requestor/ext/requestor-vertx/src/test/java/io/reinert/requestor/vertx/VertxAsyncRunnerTest.java b/requestor/ext/requestor-vertx/src/test/java/io/reinert/requestor/vertx/VertxAsyncRunnerTest.java new file mode 100644 index 000000000..03db681e1 --- /dev/null +++ b/requestor/ext/requestor-vertx/src/test/java/io/reinert/requestor/vertx/VertxAsyncRunnerTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2023 Danilo Reinert + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.reinert.requestor.vertx; + +import io.reinert.requestor.core.RequestException; +import io.reinert.requestor.core.Session; +import io.reinert.requestor.java.net.Requestor; +import io.vertx.core.Vertx; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Tests for VertxAsyncRunner + */ +public class VertxAsyncRunnerTest { + + @Test + public void testRunMethod() { + AtomicInteger counter = new AtomicInteger(); + + // Create a Vertx instance + Vertx vertx = Vertx.vertx(); + + // Create an instance of VertxAsyncRunner + VertxAsyncRunner asyncRunner = new VertxAsyncRunner(vertx); + + // Define the delay in milliseconds + long delayMillis = 2000; + + // Define the Runnable to be executed + // Perform the desired actions + Runnable runnable = counter::incrementAndGet; + + // Execute the run method + asyncRunner.run(runnable, delayMillis); + + // Sleep for a sufficient amount of time to allow the execution to complete + try { + Thread.sleep(delayMillis + 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + vertx.close(); + + Assert.assertEquals(1, counter.get()); + } + + @Test + public void testSessionRequest() throws RequestException { + AtomicInteger counter = new AtomicInteger(); + + Vertx vertx = Vertx.vertx(); + + // Integrates requestor to vertx engine + VertxAsyncRunner asyncRunner = new VertxAsyncRunner(vertx); + + // Start a new session with vertx async runner + Session session = Requestor.newSession(asyncRunner); + + session.get("https://httpbin.org/ip", String.class) + .onSuccess(counter::incrementAndGet) + .await(); // hold main thread until request is done + + vertx.close(); + + Assert.assertEquals(1, counter.get()); + } +}