Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/main/java/io/vertx/core/Future.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,20 @@ static <T> Future<T> fromCompletionStage(CompletionStage<T> completionStage, Con
return promise.future();
}

/**
@see CompletableFuture#getNow
@see #isComplete
*/
default T getNow (T valueIfAbsent) {
if (succeeded()) {
return result();
} else if (failed()) {
throw Utils.throwAsUnchecked(cause());
} else {
return valueIfAbsent; // !isComplete
}
}

/**
* Park the current thread until the {@code future} is completed, when the future
* is completed the thread is un-parked and
Expand All @@ -627,21 +641,19 @@ static <T> Future<T> fromCompletionStage(CompletionStage<T> completionStage, Con
* @throws IllegalStateException when called from an event-loop thread or a non Vert.x thread
*/
default T await() {
if (isComplete()) {
return getNow(null);
}

io.vertx.core.impl.WorkerExecutor executor = io.vertx.core.impl.WorkerExecutor.unwrapWorkerExecutor();
io.vertx.core.impl.WorkerExecutor.TaskController cont = executor.current();
onComplete(ar -> cont.resume());
try {
cont.suspendAndAwaitResume();
} catch (InterruptedException e) {
Utils.throwAsUnchecked(e.getCause());
return null;
}
if (succeeded()) {
return result();
} else {
Utils.throwAsUnchecked(cause());
return null;
throw Utils.throwAsUnchecked(e.getCause() != null ? e.getCause() : e);// should it be simply throwAsUnchecked(e) ?
}
return getNow(null);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static boolean isWindows() {
}

@SuppressWarnings("unchecked")
public static <E extends Throwable> void throwAsUnchecked(Throwable t) throws E {
public static <E extends Throwable> RuntimeException throwAsUnchecked(Throwable t) throws E {
throw (E) t;
}
}
26 changes: 26 additions & 0 deletions src/test/java/io/vertx/core/FutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.vertx.core.impl.NoStackTraceThrowable;
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.test.core.Repeat;
import org.junit.Ignore;
import org.junit.Test;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -1810,4 +1811,29 @@ private void completedFutureTimeout(Context ctx, Future<String> future) throws E
}));
await();
}

@Test @Ignore
public void npe (){
vertx.deployVerticle(new AbstractVerticle() {
@Override public void start (){
var p = Promise.promise();
var thread = Thread.currentThread();

System.out.println(thread);
thread.interrupt();

try {
p.future().await();// bug: java.lang.NullPointerException: Cannot throw exception because "t" is null
fail("must fail with InterruptedException");
} catch (Throwable e){
if (e instanceof InterruptedException){
testComplete();
} else {
fail(e);
}
}
}
}, new DeploymentOptions().setThreadingModel(ThreadingModel.VIRTUAL_THREAD));
await();
}
}