Created
August 14, 2022 16:30
-
-
Save guss77/f91cd9b857d3be131885b25e79c74fab to your computer and use it in GitHub Desktop.
Vert.x Future onSuccess/onFailure do not propagate errors
This file contains hidden or 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
package testing; | |
import io.vertx.core.Future; | |
import io.vertx.core.Promise; | |
import io.vertx.core.Vertx; | |
public class VertxFutureTest { | |
static Object waiter = new Object(); | |
static boolean done = false; | |
/** | |
* Call with either "a" or "b" as the first argument | |
* a) the flow will not complete correctly and will timeout | |
* b) the flow will complete correctly and report an error as expected | |
**/ | |
public static void main(String[] args) { | |
if (args.length < 1) args = new String[] { "help" }; | |
switch (args[0]) { | |
case "a": testA(); break; | |
case "b": testB(); break; | |
default: | |
System.err.println("Please specify test case to run - 'a' or 'b'"); | |
} | |
System.exit(0); | |
} | |
public static void testA() { | |
Promise<Void> event = Promise.promise(); | |
new Thread(() -> { | |
// mimic running the async flow from a thread you don't control, maybe Vert.x event loop | |
event.future().onSuccess(__ -> { | |
throw new RuntimeException("unexpected error"); | |
}) | |
.compose(__ -> Future.succeededFuture()) // another step to run under normal conditions | |
.onFailure(t -> System.err.println("Error occured! " + t)) // final error handler | |
.eventually(__ -> { | |
System.err.println("Done with test"); | |
finish(); | |
return Future.succeededFuture(); | |
}); | |
}).start(); | |
// cause the event to fire | |
event.complete(); | |
waitForFinish(); | |
} | |
public static void testB() { | |
Promise<Void> event = Promise.promise(); | |
new Thread(() -> { | |
// mimic running the async flow from a thread you don't control, maybe Vert.x event loop | |
event.future().<Void>map(__ -> { | |
throw new RuntimeException("unexpected error"); | |
}) | |
.compose(__ -> Future.succeededFuture()) // another step to run under normal conditions | |
.onFailure(t -> System.err.println("Error occured! " + t)) // final error handler | |
.eventually(__ -> { | |
System.err.println("Done with test"); | |
finish(); | |
return Future.succeededFuture(); | |
}); | |
}).start(); | |
// cause the event to fire | |
event.complete(); | |
waitForFinish(); | |
} | |
static synchronized void finish() { | |
synchronized (waiter) { | |
done = true; | |
waiter.notify(); | |
} | |
} | |
static void waitForFinish() { | |
synchronized (waiter) { | |
try { | |
waiter.wait(3000); | |
} catch (InterruptedException e) { | |
} | |
if (done) { | |
System.out.println("We're done"); | |
} else { | |
System.err.println("Timed out!"); | |
System.exit(5); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment