diff --git a/hugegraph-common/src/main/java/com/baidu/hugegraph/testutil/Assert.java b/hugegraph-common/src/main/java/com/baidu/hugegraph/testutil/Assert.java index 537e9d60..333002d6 100644 --- a/hugegraph-common/src/main/java/com/baidu/hugegraph/testutil/Assert.java +++ b/hugegraph-common/src/main/java/com/baidu/hugegraph/testutil/Assert.java @@ -19,6 +19,7 @@ package com.baidu.hugegraph.testutil; +import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; @@ -40,7 +41,8 @@ public interface ThrowableConsumer { public static void assertThrows(Class throwable, ThrowableRunnable runnable) { - assertThrows(throwable, runnable, e -> { + CompletableFuture future = assertThrowsFuture(throwable, runnable); + future.thenAccept(e -> { System.err.println(e); }); } @@ -48,23 +50,34 @@ public static void assertThrows(Class throwable, public static void assertThrows(Class throwable, ThrowableRunnable runnable, Consumer exceptionConsumer) { + CompletableFuture future = assertThrowsFuture(throwable, + runnable); + future.thenAccept(exceptionConsumer); + } + + public static CompletableFuture assertThrowsFuture( + Class clazz, + ThrowableRunnable runnable) { + CompletableFuture future = new CompletableFuture<>(); boolean fail = false; try { runnable.run(); fail = true; } catch (Throwable e) { - if (!throwable.isInstance(e)) { + if (!clazz.isInstance(e)) { Assert.fail(String.format( "Bad exception type %s(expected %s)", - e.getClass().getName(), throwable.getName())); + e.getClass().getName(), clazz.getName())); } - exceptionConsumer.accept(e); + future.complete(e); } if (fail) { - Assert.fail(String.format( - "No exception was thrown(expected %s)", - throwable.getName())); + String msg = String.format("No exception was thrown(expected %s)", + clazz.getName()); + future.completeExceptionally(new AssertionError(msg)); + Assert.fail(msg); } + return future; } public static void assertEquals(byte expected, Object actual) {