You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by je...@apache.org on 2022/05/18 03:27:25 UTC

[incubator-hugegraph-commons] branch assert-throws-future created (now 1ecd4cc)

This is an automated email from the ASF dual-hosted git repository.

jermy pushed a change to branch assert-throws-future
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git


      at 1ecd4cc  support assert-throws return future

This branch includes the following new commits:

     new 1ecd4cc  support assert-throws return future

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-hugegraph-commons] 01/01: support assert-throws return future

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch assert-throws-future
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-commons.git

commit 1ecd4cc29f6dc31bb8800e869fcf812d3f4a7fc4
Author: Zhangmei Li <li...@baidu.com>
AuthorDate: Wed May 18 11:24:09 2022 +0800

    support assert-throws return future
    
    Change-Id: I8fe115a7518dd84e22c9e28f777515e74e48d5d2
---
 .../java/com/baidu/hugegraph/testutil/Assert.java  | 27 ++++++++++++++++------
 1 file changed, 20 insertions(+), 7 deletions(-)

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 537e9d6..333002d 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 class Assert extends org.junit.Assert {
 
     public static void assertThrows(Class<? extends Throwable> throwable,
                                     ThrowableRunnable runnable) {
-        assertThrows(throwable, runnable, e -> {
+        CompletableFuture<?> future = assertThrowsFuture(throwable, runnable);
+        future.thenAccept(e -> {
             System.err.println(e);
         });
     }
@@ -48,23 +50,34 @@ public class Assert extends org.junit.Assert {
     public static void assertThrows(Class<? extends Throwable> throwable,
                                     ThrowableRunnable runnable,
                                     Consumer<Throwable> exceptionConsumer) {
+        CompletableFuture<Throwable> future = assertThrowsFuture(throwable,
+                                                                 runnable);
+        future.thenAccept(exceptionConsumer);
+    }
+
+    public static CompletableFuture<Throwable> assertThrowsFuture(
+                                               Class<? extends Throwable> clazz,
+                                               ThrowableRunnable runnable) {
+        CompletableFuture<Throwable> 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) {