You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/02/09 04:53:29 UTC
[dubbo] branch master updated: Fix RpcContext.asyncCall return when
throw RpcException (#5607)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 6d1afb4 Fix RpcContext.asyncCall return when throw RpcException (#5607)
6d1afb4 is described below
commit 6d1afb440768f51caf1a2190a08e182868c5a66f
Author: Mark <mr...@gmail.com>
AuthorDate: Sun Feb 9 12:53:15 2020 +0800
Fix RpcContext.asyncCall return when throw RpcException (#5607)
Fix #5606
---
.../main/java/org/apache/dubbo/rpc/RpcContext.java | 34 ++--------------------
.../java/org/apache/dubbo/rpc/RpcContextTest.java | 24 +++++++++++++--
2 files changed, 25 insertions(+), 33 deletions(-)
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
index f3792b0..bc57104 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcContext.java
@@ -30,10 +30,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
@@ -681,34 +678,9 @@ public class RpcContext {
removeAttachment(ASYNC_KEY);
}
} catch (final RpcException e) {
- return new CompletableFuture<T>() {
- @Override
- public boolean cancel(boolean mayInterruptIfRunning) {
- return false;
- }
-
- @Override
- public boolean isCancelled() {
- return false;
- }
-
- @Override
- public boolean isDone() {
- return true;
- }
-
- @Override
- public T get() throws InterruptedException, ExecutionException {
- throw new ExecutionException(e.getCause());
- }
-
- @Override
- public T get(long timeout, TimeUnit unit)
- throws InterruptedException, ExecutionException,
- TimeoutException {
- return get();
- }
- };
+ CompletableFuture<T> exceptionFuture = new CompletableFuture<>();
+ exceptionFuture.completeExceptionally(e);
+ return exceptionFuture;
}
return ((CompletableFuture<T>) getContext().getFuture());
}
diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java
index df06aec..3b6dd0e 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java
@@ -17,12 +17,13 @@
package org.apache.dubbo.rpc;
import org.apache.dubbo.common.URL;
-
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
public class RpcContextTest {
@@ -149,10 +150,29 @@ public class RpcContextTest {
Assertions.assertTrue(rpcContext.isAsyncStarted());
asyncContext.write(new Object());
- Assertions.assertTrue(((AsyncContextImpl)asyncContext).getInternalFuture().isDone());
+ Assertions.assertTrue(((AsyncContextImpl) asyncContext).getInternalFuture().isDone());
rpcContext.stopAsync();
Assertions.assertTrue(rpcContext.isAsyncStarted());
}
+ @Test
+ public void testAsyncCall() {
+ CompletableFuture<String> rpcFuture = RpcContext.getContext().asyncCall(() -> {
+ throw new NullPointerException();
+ });
+
+ rpcFuture.whenComplete((rpcResult, throwable) -> {
+ System.out.println(throwable.toString());
+ Assertions.assertNull(rpcResult);
+ Assertions.assertTrue(throwable instanceof RpcException);
+ Assertions.assertTrue(throwable.getCause() instanceof NullPointerException);
+ });
+
+ Assertions.assertThrows(ExecutionException.class, rpcFuture::get);
+
+ rpcFuture = rpcFuture.exceptionally(throwable -> "mock success");
+
+ Assertions.assertEquals("mock success", rpcFuture.join());
+ }
}