Skip to content

Commit

Permalink
#147 [vrtx] Introduce VertxAsyncRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
reinert committed Apr 29, 2023
1 parent e4672e8 commit cd4254b
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit cd4254b

Please sign in to comment.