Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #25 from vert-x3/fix/awaitResult-timeout-tests
Rework of the test cases for awaitResult and timeouts
  • Loading branch information
jponge committed Apr 5, 2018
2 parents d2b1ff2 + f54c641 commit dd71460
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
8 changes: 6 additions & 2 deletions src/main/java/io/vertx/ext/sync/Sync.java
Expand Up @@ -37,6 +37,7 @@ public static <T> T awaitResult(Consumer<Handler<AsyncResult<T>>> consumer) {
try {
return new AsyncAdaptor<T>() {
@Override
@Suspendable
protected void requestAsync() {
try {
consumer.accept(this);
Expand All @@ -49,7 +50,7 @@ protected void requestAsync() {
throw new VertxException(t);
}
}

/**
* Invoke an asynchronous operation and obtain the result synchronous.
* The fiber will be blocked until the result is available. No kernel thread is blocked.
Expand All @@ -64,6 +65,7 @@ public static <T> T awaitResult(Consumer<Handler<AsyncResult<T>>> consumer, long
try {
return new AsyncAdaptor<T>() {
@Override
@Suspendable
protected void requestAsync() {
try {
consumer.accept(this);
Expand Down Expand Up @@ -92,6 +94,7 @@ public static <T> T awaitEvent(Consumer<Handler<T>> consumer) {
try {
return new HandlerAdaptor<T>() {
@Override
@Suspendable
protected void requestAsync() {
try {
consumer.accept(this);
Expand All @@ -104,7 +107,7 @@ protected void requestAsync() {
throw new VertxException(t);
}
}

/**
* Receive a single event from a handler synchronously.
* The fiber will be blocked until the event occurs. No kernel thread is blocked.
Expand All @@ -118,6 +121,7 @@ protected void requestAsync() {
public static <T> T awaitEvent(Consumer<Handler<T>> consumer, long timeout) {
try {
return new HandlerAdaptor<T>() {
@Suspendable
@Override
protected void requestAsync() {
try {
Expand Down
62 changes: 40 additions & 22 deletions src/test/java/io/vertx/ext/sync/test/TestVerticle.java
@@ -1,5 +1,6 @@
package io.vertx.ext.sync.test;

import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.fibers.Suspendable;
import co.paralleluniverse.strands.Strand;
import co.paralleluniverse.strands.channels.Channel;
Expand Down Expand Up @@ -30,7 +31,6 @@
import static org.hamcrest.core.Is.*;

/**
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class TestVerticle extends SyncVerticle {
Expand Down Expand Up @@ -112,13 +112,13 @@ protected void testFiberHandler() {
server.listen(res -> {
assertTrue(res.succeeded());
HttpClient client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
client.getNow("/somepath", resp -> {
assertTrue(resp.statusCode() == 200);
client.close();
server.close(res2 -> {
complete();
});
client.getNow("/somepath", resp -> {
assertTrue(resp.statusCode() == 200);
client.close();
server.close(res2 -> {
complete();
});
});
});
}

Expand Down Expand Up @@ -151,17 +151,35 @@ protected void testExecSyncMethodWithNoParamsAndHandlerWithReturn() {
assertEquals("wibble", res);
complete();
}


@Suspendable
private void sleep(long millis) {
try {
Strand.sleep(millis);
} catch (SuspendExecution | InterruptedException suspendExecution) {
throw new AssertionError(); // Required to compile, but the try-catch block is reworked by the Quasar instrumentation
}
}

@Suspendable
protected void testExecSyncMethodWithNoParamsAndHandlerWithReturnNoTimeout() {
String res = awaitResult(h -> ai.methodWithNoParamsAndHandlerWithReturnTimeout(h, 1000), 2000);
long start = System.currentTimeMillis();
String res = awaitResult(h -> {
sleep(500);
ai.methodWithNoParamsAndHandlerWithReturn(h);
}, 2000);
long duration = (System.currentTimeMillis() - start);
assertEquals("wibble", res);
assertTrue(duration > 500 && duration < 1000);
complete();
}

@Suspendable
protected void testExecSyncMethodWithNoParamsAndHandlerWithReturnTimedout() {
String res = awaitResult(h -> ai.methodWithNoParamsAndHandlerWithReturnTimeout(h, 1000), 500);
String res = awaitResult(h -> {
sleep(1000);
ai.methodWithNoParamsAndHandlerWithReturn(h);
}, 500);
assertNull(res);
complete();
}
Expand All @@ -182,7 +200,7 @@ protected void testExecSyncMethodThatFails() {
fail("Should throw exception");
} catch (Exception e) {
assertTrue(e instanceof VertxException);
VertxException ve = (VertxException)e;
VertxException ve = (VertxException) e;
assertEquals("oranges", ve.getCause().getMessage());
complete();
}
Expand All @@ -200,28 +218,28 @@ protected void testReceiveEvent() {

complete();
}

@Suspendable
protected void testReceiveEventTimedout() {

long start = System.currentTimeMillis();
try {
long tid = awaitEvent(h -> vertx.setTimer(500, h), 250);
} catch(NullPointerException npe) {
assertThat(npe, isA(NullPointerException.class));
} catch(Exception e) {
assertTrue(false);
long tid = awaitEvent(h -> vertx.setTimer(500, h), 250);
} catch (NullPointerException npe) {
assertThat(npe, isA(NullPointerException.class));
} catch (Exception e) {
assertTrue(false);
} finally {
complete();
}
complete();
}
}

@Suspendable
protected void testReceiveEventNoTimeout() {

long start = System.currentTimeMillis();
long tid = awaitEvent(h -> vertx.setTimer(500, h), 1000);
long end = System.currentTimeMillis();
long end = System.currentTimeMillis();
assertTrue(end - start >= 500);
assertTrue(tid >= 0);

Expand Down
2 changes: 0 additions & 2 deletions src/test/java/io/vertx/ext/sync/testmodel/AsyncInterface.java
Expand Up @@ -27,7 +27,5 @@ public interface AsyncInterface {

void methodThatFails(String foo, Handler<AsyncResult<String>> resultHandler);

String methodWithNoParamsAndHandlerWithReturnTimeout(Handler<AsyncResult<String>> resultHandler, long timeout);


}
Expand Up @@ -54,13 +54,4 @@ public void methodThatFails(String foo, Handler<AsyncResult<String>> resultHandl
vertx.runOnContext(v -> resultHandler.handle(Future.failedFuture(new Exception(foo))));
}

@Override
public String methodWithNoParamsAndHandlerWithReturnTimeout(Handler<AsyncResult<String>> resultHandler, long timeout) {
try {
Thread.sleep(timeout);
} catch(InterruptedException e) {
}
vertx.runOnContext(v -> resultHandler.handle(Future.succeededFuture("wibble")));
return "flooble";
}
}

0 comments on commit dd71460

Please sign in to comment.