-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#147 [vrtx] Introduce VertxAsyncRunner
- Loading branch information
Showing
2 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
requestor/ext/requestor-vertx/src/main/java/io/reinert/requestor/vertx/VertxAsyncRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...or/ext/requestor-vertx/src/test/java/io/reinert/requestor/vertx/VertxAsyncRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |