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

iluo 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 0f85bbd  [Dubbo-4585] fix when provider throw RpcException consumer do 
not invoke mock (#4586)
0f85bbd is described below

commit 0f85bbd66e3592029cf67ddb6d3242092417307e
Author: codeimport <[email protected]>
AuthorDate: Thu Jul 18 11:40:08 2019 +0800

    [Dubbo-4585] fix when provider throw RpcException consumer do not invoke 
mock (#4586)
    
    * fix #4585 when provider throw RpcException consumer invoke mock
    
    * fix #4585 when provider throw RpcException consumer invoke mock
    
    * modify unit test
---
 .../support/wrapper/MockClusterInvoker.java        |  11 +
 .../support/wrapper/MockClusterInvokerTest.java    |  10 +
 .../wrapper/MockProviderRpcExceptionTest.java      | 238 +++++++++++++++++++++
 3 files changed, 259 insertions(+)

diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java
index 77eceb0..140751f 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java
@@ -86,6 +86,17 @@ public class MockClusterInvoker<T> implements Invoker<T> {
             //fail-mock
             try {
                 result = this.invoker.invoke(invocation);
+
+                //fix:#4585
+                if(result.getException() != null && result.getException() 
instanceof RpcException){
+                    RpcException rpcException= 
(RpcException)result.getException();
+                    if(rpcException.isBiz()){
+                        throw  rpcException;
+                    }else {
+                        result = doMockInvoke(invocation, rpcException);
+                    }
+                }
+
             } catch (RpcException e) {
                 if (e.isBiz()) {
                     throw e;
diff --git 
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java
 
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java
index c133d35..57aca20 100644
--- 
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java
+++ 
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvokerTest.java
@@ -680,6 +680,8 @@ public class MockClusterInvokerTest {
 
         String getSomething3();
 
+        String getSomething4();
+
         int getInt1();
 
         boolean getBoolean1();
@@ -706,6 +708,10 @@ public class MockClusterInvokerTest {
             return "something3";
         }
 
+        public String getSomething4(){
+            throw new RpcException("getSomething4|RpcException");
+        }
+
         public int getInt1() {
             return 1;
         }
@@ -748,6 +754,10 @@ public class MockClusterInvokerTest {
             return "something3mock";
         }
 
+        public String getSomething4(){
+            return "something4mock";
+        }
+
         public List<String> getListString() {
             return Arrays.asList(new String[]{"Tommock", "Jerrymock"});
         }
diff --git 
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockProviderRpcExceptionTest.java
 
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockProviderRpcExceptionTest.java
new file mode 100644
index 0000000..39459fb
--- /dev/null
+++ 
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockProviderRpcExceptionTest.java
@@ -0,0 +1,238 @@
+/*
+ * 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.cluster.support.wrapper;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.cluster.LoadBalance;
+import org.apache.dubbo.rpc.cluster.directory.StaticDirectory;
+import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.dubbo.rpc.Constants.MOCK_KEY;
+
+public class MockProviderRpcExceptionTest {
+
+    List<Invoker<IHelloRpcService>> invokers = new 
ArrayList<Invoker<IHelloRpcService>>();
+
+    @BeforeEach
+    public void beforeMethod() {
+        invokers.clear();
+    }
+
+    /**
+     * Test if mock policy works fine: ProviderRpcException
+     */
+    @Test
+    public void testMockInvokerProviderRpcException() {
+        URL url = URL.valueOf("remote://1.2.3.4/" + 
IHelloRpcService.class.getName());
+        url = url.addParameter(MOCK_KEY, 
"true").addParameter("invoke_return_error", "true");
+        Invoker<IHelloRpcService> cluster = getClusterInvoker(url);
+        RpcInvocation invocation = new RpcInvocation();
+        invocation.setMethodName("getSomething4");
+        Result ret = cluster.invoke(invocation);
+        Assertions.assertEquals("something4mock", ret.getValue());
+
+    }
+
+
+    private Invoker<IHelloRpcService> getClusterInvokerMock(URL url, 
Invoker<IHelloRpcService> mockInvoker) {
+        // As `javassist` have a strict restriction of argument types, request 
will fail if Invocation do not contains complete parameter type information
+        final URL durl = url.addParameter("proxy", "jdk");
+        invokers.clear();
+        ProxyFactory proxy = 
ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
+        Invoker<IHelloRpcService> invoker1 = proxy.getInvoker(new 
HelloRpcService(), IHelloRpcService.class, durl);
+        invokers.add(invoker1);
+        if (mockInvoker != null) {
+            invokers.add(mockInvoker);
+        }
+
+        StaticDirectory<IHelloRpcService> dic = new 
StaticDirectory<IHelloRpcService>(durl, invokers, null);
+        dic.buildRouterChain();
+        AbstractClusterInvoker<IHelloRpcService> cluster = new 
AbstractClusterInvoker(dic) {
+            @Override
+            protected Result doInvoke(Invocation invocation, List invokers, 
LoadBalance loadbalance)
+                    throws RpcException {
+                if (durl.getParameter("invoke_return_error", false)) {
+                    throw new RpcException(RpcException.TIMEOUT_EXCEPTION, 
"test rpc exception ");
+                } else {
+                    return ((Invoker<?>) invokers.get(0)).invoke(invocation);
+                }
+            }
+        };
+        return new MockClusterInvoker<IHelloRpcService>(dic, cluster);
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private Invoker<IHelloRpcService> getClusterInvoker(URL url) {
+        return getClusterInvokerMock(url, null);
+    }
+
+    public static interface IHelloRpcService {
+        String getSomething();
+
+        String getSomething2();
+
+        String getSomething3();
+
+        String getSomething4();
+
+        int getInt1();
+
+        boolean getBoolean1();
+
+        Boolean getBoolean2();
+
+        public List<String> getListString();
+
+        public List<User> getUsers();
+
+        void sayHello();
+    }
+
+    public static class HelloRpcService implements IHelloRpcService {
+        public String getSomething() {
+            return "something";
+        }
+
+        public String getSomething2() {
+            return "something2";
+        }
+
+        public String getSomething3() {
+            return "something3";
+        }
+
+        public String getSomething4(){
+            throw new RpcException("getSomething4|RpcException");
+        }
+
+        public int getInt1() {
+            return 1;
+        }
+
+        public boolean getBoolean1() {
+            return false;
+        }
+
+        public Boolean getBoolean2() {
+            return Boolean.FALSE;
+        }
+
+        public List<String> getListString() {
+            return Arrays.asList(new String[]{"Tom", "Jerry"});
+        }
+
+        public List<User> getUsers() {
+            return Arrays.asList(new User[]{new User(1, "Tom"), new User(2, 
"Jerry")});
+        }
+
+        public void sayHello() {
+            System.out.println("hello prety");
+        }
+    }
+
+    public static class IHelloRpcServiceMock implements IHelloRpcService {
+        public IHelloRpcServiceMock() {
+
+        }
+
+        public String getSomething() {
+            return "somethingmock";
+        }
+
+        public String getSomething2() {
+            return "something2mock";
+        }
+
+        public String getSomething3() {
+            return "something3mock";
+        }
+
+        public String getSomething4(){
+            return "something4mock";
+        }
+
+        public List<String> getListString() {
+            return Arrays.asList(new String[]{"Tommock", "Jerrymock"});
+        }
+
+        public List<User> getUsers() {
+            return Arrays.asList(new User[]{new User(1, "Tommock"), new 
User(2, "Jerrymock")});
+        }
+
+        public int getInt1() {
+            return 1;
+        }
+
+        public boolean getBoolean1() {
+            return false;
+        }
+
+        public Boolean getBoolean2() {
+            return Boolean.FALSE;
+        }
+
+        public void sayHello() {
+            System.out.println("hello prety");
+        }
+    }
+
+    public static class User {
+        private int id;
+        private String name;
+
+        public User() {
+        }
+
+        public User(int id, String name) {
+            super();
+            this.id = id;
+            this.name = name;
+        }
+
+        public int getId() {
+            return id;
+        }
+
+        public void setId(int id) {
+            this.id = id;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+    }
+}

Reply via email to