You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/09/21 16:04:48 UTC

[dubbo] branch 3.0 updated: [3.0] Enhance ForkingClusterInvoker unit test (#8865)

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

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new aed194a  [3.0] Enhance ForkingClusterInvoker unit test (#8865)
aed194a is described below

commit aed194a9a71829be0beb4b23795bc0c024117bd9
Author: Wang Chengming <63...@qq.com>
AuthorDate: Wed Sep 22 00:04:37 2021 +0800

    [3.0] Enhance ForkingClusterInvoker unit test (#8865)
    
    2.pretty code
---
 .../rpc/cluster/support/ForkingClusterInvoker.java | 11 +++++----
 .../cluster/support/ForkingClusterInvokerTest.java | 27 ++++++++++++++++------
 2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
index bf69820..8980b34 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
@@ -42,7 +42,7 @@ import static org.apache.dubbo.rpc.cluster.Constants.DEFAULT_FORKS;
 
 /**
  * NOTICE! This implementation does not work well with async call.
- *
+ * <p>
  * Invoke a specific number of invokers concurrently, usually used for demanding real-time operations, but need to waste more service resources.
  *
  * <a href="http://en.wikipedia.org/wiki/Fork_(topology)">Fork</a>
@@ -54,7 +54,7 @@ public class ForkingClusterInvoker<T> extends AbstractClusterInvoker<T> {
      * which with the use of {@link org.apache.dubbo.common.threadlocal.InternalThreadLocal} in {@link RpcContext}.
      */
     private final ExecutorService executor = Executors.newCachedThreadPool(
-            new NamedInternalThreadFactory("forking-cluster-timer", true));
+        new NamedInternalThreadFactory("forking-cluster-timer", true));
 
     public ForkingClusterInvoker(Directory<T> directory) {
         super(directory);
@@ -101,11 +101,14 @@ public class ForkingClusterInvoker<T> extends AbstractClusterInvoker<T> {
                 Object ret = ref.poll(timeout, TimeUnit.MILLISECONDS);
                 if (ret instanceof Throwable) {
                     Throwable e = (Throwable) ret;
-                    throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
+                    throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : RpcException.UNKNOWN_EXCEPTION,
+                        "Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. " +
+                            "Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);
                 }
                 return (Result) ret;
             } catch (InterruptedException e) {
-                throw new RpcException("Failed to forking invoke provider " + selected + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e);
+                throw new RpcException("Failed to forking invoke provider " + selected + ", " +
+                    "but no luck to perform the invocation. Last error is: " + e.getMessage(), e);
             }
         } finally {
             // clear attachments which is binding to current thread.
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java
index c5469f1..17d6905 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java
@@ -34,6 +34,7 @@ import java.util.List;
 import java.util.Map;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.BDDMockito.given;
 import static org.mockito.Mockito.mock;
 
@@ -43,7 +44,7 @@ import static org.mockito.Mockito.mock;
 @SuppressWarnings("unchecked")
 public class ForkingClusterInvokerTest {
 
-    private List<Invoker<ForkingClusterInvokerTest>> invokers = new ArrayList<Invoker<ForkingClusterInvokerTest>>();
+    private List<Invoker<ForkingClusterInvokerTest>> invokers = new ArrayList<>();
     private URL url = URL.valueOf("test://test:11/test?forks=2");
     private Invoker<ForkingClusterInvokerTest> invoker1 = mock(Invoker.class);
     private Invoker<ForkingClusterInvokerTest> invoker2 = mock(Invoker.class);
@@ -107,8 +108,7 @@ public class ForkingClusterInvokerTest {
     @Test
     public void testInvokeException() {
         resetInvokerToException();
-        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<ForkingClusterInvokerTest>(
-                dic);
+        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<>(dic);
 
         try {
             invoker.invoke(invocation);
@@ -122,8 +122,7 @@ public class ForkingClusterInvokerTest {
     @Test
     public void testClearRpcContext() {
         resetInvokerToException();
-        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<ForkingClusterInvokerTest>(
-                dic);
+        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<>(dic);
 
         String attachKey = "attach";
         String attachValue = "value";
@@ -148,8 +147,22 @@ public class ForkingClusterInvokerTest {
 
         resetInvokerToNoException();
 
-        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<ForkingClusterInvokerTest>(
-                dic);
+        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<>(dic);
+        Result ret = invoker.invoke(invocation);
+        Assertions.assertSame(result, ret);
+    }
+
+    @Test()
+    public void testInvokeWithIllegalForksParam() {
+        URL url = URL.valueOf("test://test:11/test?forks=-1");
+        given(dic.getUrl()).willReturn(url);
+        given(dic.getConsumerUrl()).willReturn(url);
+        given(invoker1.invoke(invocation)).willReturn(result);
+        given(invoker1.getUrl()).willReturn(url);
+        given(invoker1.isAvailable()).willReturn(true);
+        given(invoker1.getInterface()).willReturn(ForkingClusterInvokerTest.class);
+
+        ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<>(dic);
         Result ret = invoker.invoke(invocation);
         Assertions.assertSame(result, ret);
     }