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 2019/07/09 08:03:52 UTC

[dubbo] branch 2.7.3-release updated: RpcContext cannot getFuture after setFuture (#4502)

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

liujun pushed a commit to branch 2.7.3-release
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/2.7.3-release by this push:
     new 6e20848  RpcContext cannot getFuture after setFuture (#4502)
6e20848 is described below

commit 6e20848a72b068461ef10606ee6b8c4221cd696c
Author: Haiyang <ha...@163.com>
AuthorDate: Tue Jul 9 16:03:41 2019 +0800

    RpcContext cannot getFuture after setFuture (#4502)
    
    fixes https://github.com/apache/dubbo/issues/4501
---
 .../java/com/alibaba/dubbo/rpc/RpcContext.java     | 11 +++++-
 .../java/org/apache/dubbo/rpc/RpcContextTest.java  | 46 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java
index c21b701..5fcf498 100644
--- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java
+++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/RpcContext.java
@@ -27,7 +27,6 @@ import java.util.concurrent.Future;
 @Deprecated
 public class RpcContext extends org.apache.dubbo.rpc.RpcContext {
 
-
     public static RpcContext getContext() {
         return newInstance(org.apache.dubbo.rpc.RpcContext.getContext());
     }
@@ -36,7 +35,9 @@ public class RpcContext extends org.apache.dubbo.rpc.RpcContext {
         RpcContext copy = new RpcContext();
         copy.getAttachments().putAll(rpcContext.getAttachments());
         copy.get().putAll(rpcContext.get());
-        copy.setFuture(rpcContext.getCompletableFuture());
+        if (rpcContext.getCompletableFuture() != null) {
+            copy.setFuture(rpcContext.getCompletableFuture());
+        }
         copy.setUrls(rpcContext.getUrls());
         copy.setUrl(rpcContext.getUrl());
         copy.setMethodName(rpcContext.getMethodName());
@@ -56,6 +57,7 @@ public class RpcContext extends org.apache.dubbo.rpc.RpcContext {
         return copy;
     }
 
+    @Override
     public <T> Future<T> getFuture() {
         CompletableFuture completableFuture = FutureContext.getContext().getCompatibleCompletableFuture();
         if (completableFuture == null) {
@@ -63,4 +65,9 @@ public class RpcContext extends org.apache.dubbo.rpc.RpcContext {
         }
         return new FutureAdapter(completableFuture);
     }
+
+    @Override
+    public void setFuture(CompletableFuture<?> future) {
+        FutureContext.getContext().setCompatibleFuture(future);
+    }
 }
diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java
new file mode 100644
index 0000000..4a8d61e
--- /dev/null
+++ b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/RpcContextTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.rpc;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+import org.junit.jupiter.api.Test;
+import com.alibaba.dubbo.rpc.RpcContext;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RpcContextTest {
+
+    @Test
+    public void testSetFuture() {
+        CompletableFuture completableFuture = new CompletableFuture();
+        RpcContext.getContext().setFuture(completableFuture);
+
+        CompletableFuture result = FutureContext.getContext().getCompatibleCompletableFuture();
+        assertEquals(result, completableFuture);
+    }
+
+    @Test
+    public void testSetFutureAlibaba() {
+        CompletableFuture completableFuture = new CompletableFuture();
+        RpcContext.getContext().setFuture(completableFuture);
+
+        Future future = RpcContext.getContext().getFuture();
+
+        System.out.println(future);
+    }
+}