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:10 UTC
[dubbo] branch 3.0 updated: [3.0] Enhance
FailfastClusterInvokerTest unit test (#8864)
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 118d168 [3.0] Enhance FailfastClusterInvokerTest unit test (#8864)
118d168 is described below
commit 118d16815137dd1024f25d34b35646e56b7663f7
Author: Wang Chengming <63...@qq.com>
AuthorDate: Wed Sep 22 00:03:57 2021 +0800
[3.0] Enhance FailfastClusterInvokerTest unit test (#8864)
2.pretty code
---
.../cluster/support/FailfastClusterInvoker.java | 13 +++++-----
.../support/FailfastClusterInvokerTest.java | 30 +++++++++++++++++-----
2 files changed, 30 insertions(+), 13 deletions(-)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
index a375a1a..0b9f6bd 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
@@ -32,7 +32,6 @@ import java.util.List;
* Usually used for non-idempotent write operations
*
* <a href="http://en.wikipedia.org/wiki/Fail-fast">Fail-fast</a>
- *
*/
public class FailfastClusterInvoker<T> extends AbstractClusterInvoker<T> {
@@ -51,12 +50,12 @@ public class FailfastClusterInvoker<T> extends AbstractClusterInvoker<T> {
throw (RpcException) e;
}
throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0,
- "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName()
- + " for service " + getInterface().getName()
- + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost()
- + " use dubbo version " + Version.getVersion()
- + ", but no luck to perform the invocation. Last error is: " + e.getMessage(),
- e.getCause() != null ? e.getCause() : e);
+ "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName()
+ + " for service " + getInterface().getName()
+ + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost()
+ + " use dubbo version " + Version.getVersion()
+ + ", but no luck to perform the invocation. Last error is: " + e.getMessage(),
+ e.getCause() != null ? e.getCause() : e);
}
}
}
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java
index 525f898..1b9c691 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java
@@ -32,7 +32,9 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -42,7 +44,7 @@ import static org.mockito.Mockito.mock;
*/
@SuppressWarnings("unchecked")
public class FailfastClusterInvokerTest {
- List<Invoker<FailfastClusterInvokerTest>> invokers = new ArrayList<Invoker<FailfastClusterInvokerTest>>();
+ List<Invoker<FailfastClusterInvokerTest>> invokers = new ArrayList<>();
URL url = URL.valueOf("test://test:11/test");
Invoker<FailfastClusterInvokerTest> invoker1 = mock(Invoker.class);
RpcInvocation invocation = new RpcInvocation();
@@ -84,20 +86,36 @@ public class FailfastClusterInvokerTest {
public void testInvokeException() {
Assertions.assertThrows(RpcException.class, () -> {
resetInvoker1ToException();
- FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<FailfastClusterInvokerTest>(dic);
+ FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<>(dic);
invoker.invoke(invocation);
- Assertions.assertSame(invoker1, RpcContext.getServiceContext().getInvoker());
+ assertSame(invoker1, RpcContext.getServiceContext().getInvoker());
});
}
+ @Test
+ public void testInvokeBizException() {
+ given(invoker1.invoke(invocation)).willThrow(new RpcException(RpcException.BIZ_EXCEPTION));
+ given(invoker1.getUrl()).willReturn(url);
+ given(invoker1.getInterface()).willReturn(FailfastClusterInvokerTest.class);
+ FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<>(dic);
+
+ try {
+ Result ret = invoker.invoke(invocation);
+ assertSame(result, ret);
+ fail();
+ } catch (RpcException expected) {
+ assertEquals(expected.getCode(), RpcException.BIZ_EXCEPTION);
+ }
+ }
+
@Test()
public void testInvokeNoException() {
resetInvoker1ToNoException();
- FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<FailfastClusterInvokerTest>(dic);
+ FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<>(dic);
Result ret = invoker.invoke(invocation);
- Assertions.assertSame(result, ret);
+ assertSame(result, ret);
}
@Test()
@@ -115,7 +133,7 @@ public class FailfastClusterInvokerTest {
resetInvoker1ToNoException();
- FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<FailfastClusterInvokerTest>(dic);
+ FailfastClusterInvoker<FailfastClusterInvokerTest> invoker = new FailfastClusterInvoker<>(dic);
try {
invoker.invoke(invocation);
fail();