This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 49dd3d0236 Update StubInvocationUtilTest.java (#13174)
49dd3d0236 is described below
commit 49dd3d0236b66f6154c3698375d97391429a0225
Author: gzhao9 <[email protected]>
AuthorDate: Mon Oct 9 22:12:19 2023 -0400
Update StubInvocationUtilTest.java (#13174)
---
.../dubbo/rpc/stub/StubInvocationUtilTest.java | 236 +++++++--------------
1 file changed, 80 insertions(+), 156 deletions(-)
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java
index 56a192c319..9db612a3ef 100644
---
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java
+++
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/stub/StubInvocationUtilTest.java
@@ -28,6 +28,7 @@ import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.protocol.tri.support.IGreeter;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@@ -40,110 +41,83 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
class StubInvocationUtilTest {
+ private Invoker<IGreeter> invoker;
+ private MethodDescriptor method;
+ private String request = "request";
+ private String response = "response";
+ private Result result;
- @Test
- void unaryCall() throws Throwable {
+ private Invoker<IGreeter> createMockInvoker(URL url) {
Invoker<IGreeter> invoker = Mockito.mock(Invoker.class);
+ when(invoker.getUrl()).thenReturn(url);
+ when(invoker.getInterface()).thenReturn(IGreeter.class);
+ return invoker;
+ }
+
+ private URL createMockURL(ConsumerModel consumerModel) {
URL url = Mockito.mock(URL.class);
+ when(url.getServiceModel()).thenReturn(consumerModel);
+ when(url.getServiceInterface()).thenReturn(IGreeter.class.getName());
+ when(url.getProtocolServiceKey()).thenReturn(IGreeter.class.getName());
+ return url;
+ }
+
+ private ConsumerModel createMockConsumerModel(ServiceDescriptor
serviceDescriptor) {
ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class);
- ServiceDescriptor serviceDescriptor =
Mockito.mock(ServiceDescriptor.class);
when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor);
- when(url.getServiceModel())
- .thenReturn(consumerModel);
- when(url.getServiceInterface())
- .thenReturn(IGreeter.class.getName());
- when(url.getProtocolServiceKey())
- .thenReturn(IGreeter.class.getName());
- when(invoker.getUrl())
- .thenReturn(url);
- when(invoker.getInterface())
- .thenReturn(IGreeter.class);
+ return consumerModel;
+ }
+
+ private Result createMockResult(String response) throws Throwable {
Result result = Mockito.mock(Result.class);
- when(invoker.invoke(any(Invocation.class)))
- .thenReturn(result);
- String response = "response";
when(result.recreate()).thenReturn(response);
+ return result;
+ }
+
+ private MethodDescriptor createMockMethodDescriptor() {
MethodDescriptor method = Mockito.mock(MethodDescriptor.class);
- when(method.getParameterClasses())
- .thenReturn(new Class[]{String.class});
- when(method.getMethodName())
- .thenReturn("sayHello");
- String request = "request";
+ when(method.getParameterClasses()).thenReturn(new
Class[]{String.class});
+ when(method.getMethodName()).thenReturn("sayHello");
+ return method;
+ }
+
+ @BeforeEach
+ void init() throws Throwable {
+ ServiceDescriptor serviceDescriptor =
Mockito.mock(ServiceDescriptor.class);
+ ConsumerModel consumerModel =
createMockConsumerModel(serviceDescriptor);
+ URL url = createMockURL(consumerModel);
+ invoker = createMockInvoker(url);
+ result = createMockResult("response");
+ method = createMockMethodDescriptor();
+ }
+
+ @Test
+ void unaryCall() {
+ when(invoker.invoke(any(Invocation.class))).thenReturn(result);
Object ret = StubInvocationUtil.unaryCall(invoker, method, request);
Assertions.assertEquals(response, ret);
}
@Test
- void unaryCall2() throws Throwable {
- Invoker<IGreeter> invoker = Mockito.mock(Invoker.class);
- URL url = Mockito.mock(URL.class);
- ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class);
- ServiceDescriptor serviceDescriptor =
Mockito.mock(ServiceDescriptor.class);
- when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor);
- when(url.getServiceModel())
- .thenReturn(consumerModel);
- when(url.getServiceInterface())
- .thenReturn(IGreeter.class.getName());
- when(url.getProtocolServiceKey())
- .thenReturn(IGreeter.class.getName());
- when(invoker.getUrl())
- .thenReturn(url);
- when(invoker.getInterface())
- .thenReturn(IGreeter.class);
- Result result = Mockito.mock(Result.class);
- when(invoker.invoke(any(Invocation.class)))
- .thenThrow(new RuntimeException("a"))
- .thenThrow(new Error("b"));
- String response = "response";
- when(result.recreate()).thenReturn(response);
- MethodDescriptor method = Mockito.mock(MethodDescriptor.class);
- when(method.getParameterClasses())
- .thenReturn(new Class[]{String.class});
- when(method.getMethodName())
- .thenReturn("sayHello");
- String request = "request";
+ void unaryCall2(){
+ when(invoker.invoke(any(Invocation.class))).thenThrow(new
RuntimeException("a")).thenThrow(new Error("b"));
try {
StubInvocationUtil.unaryCall(invoker, method, request);
fail();
- }catch (Throwable t){
+ } catch (Throwable t) {
// pass
}
try {
StubInvocationUtil.unaryCall(invoker, method, request);
fail();
- }catch (Throwable t){
+ } catch (Throwable t) {
// pass
}
}
@Test
void testUnaryCall() throws Throwable {
- Invoker<IGreeter> invoker = Mockito.mock(Invoker.class);
- URL url = Mockito.mock(URL.class);
- ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class);
- ServiceDescriptor serviceDescriptor =
Mockito.mock(ServiceDescriptor.class);
- when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor);
- when(url.getServiceModel())
- .thenReturn(consumerModel);
- when(url.getServiceInterface())
- .thenReturn(IGreeter.class.getName());
- when(url.getProtocolServiceKey())
- .thenReturn(IGreeter.class.getName());
- when(invoker.getUrl())
- .thenReturn(url);
- when(invoker.getInterface())
- .thenReturn(IGreeter.class);
- Result result = Mockito.mock(Result.class);
- String response = "response";
- when(invoker.invoke(any(Invocation.class)))
- .then(invocationOnMock -> result);
- when(result.recreate()).thenReturn(response);
- MethodDescriptor method = Mockito.mock(MethodDescriptor.class);
- when(method.getParameterClasses())
- .thenReturn(new Class[]{String.class});
- when(method.getMethodName())
- .thenReturn("sayHello");
- String request = "request";
+ when(invoker.invoke(any(Invocation.class))).then(invocationOnMock ->
result);
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Object> atomicReference = new AtomicReference<>();
StreamObserver<Object> responseObserver = new StreamObserver<Object>()
{
@@ -168,54 +142,29 @@ class StubInvocationUtilTest {
@Test
void biOrClientStreamCall() throws InterruptedException {
- Invoker<IGreeter> invoker = Mockito.mock(Invoker.class);
- URL url = Mockito.mock(URL.class);
- ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class);
- ServiceDescriptor serviceDescriptor =
Mockito.mock(ServiceDescriptor.class);
- when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor);
- when(url.getServiceModel())
- .thenReturn(consumerModel);
- when(url.getServiceInterface())
- .thenReturn(IGreeter.class.getName());
- when(url.getProtocolServiceKey())
- .thenReturn(IGreeter.class.getName());
- when(invoker.getUrl())
- .thenReturn(url);
- when(invoker.getInterface())
- .thenReturn(IGreeter.class);
- Result result = Mockito.mock(Result.class);
- String response = "response";
-
- when(invoker.invoke(any(Invocation.class)))
- .then(invocationOnMock -> {
- Invocation invocation = (Invocation)
invocationOnMock.getArguments()[0];
- StreamObserver<Object> observer = (StreamObserver<Object>)
invocation.getArguments()[0];
- observer.onNext(response);
- observer.onCompleted();
- when(result.recreate()).then(invocationOnMock1 -> new
StreamObserver<Object>() {
- @Override
- public void onNext(Object data) {
- observer.onNext(data);
- }
+ when(invoker.invoke(any(Invocation.class))).then(invocationOnMock -> {
+ Invocation invocation = (Invocation)
invocationOnMock.getArguments()[0];
+ StreamObserver<Object> observer = (StreamObserver<Object>)
invocation.getArguments()[0];
+ observer.onNext(response);
+ observer.onCompleted();
+ when(result.recreate()).then(invocationOnMock1 -> new
StreamObserver<Object>() {
+ @Override
+ public void onNext(Object data) {
+ observer.onNext(data);
+ }
- @Override
- public void onError(Throwable throwable) {
+ @Override
+ public void onError(Throwable throwable) {
- }
+ }
- @Override
- public void onCompleted() {
- observer.onCompleted();
- }
- });
- return result;
+ @Override
+ public void onCompleted() {
+ observer.onCompleted();
+ }
});
- MethodDescriptor method = Mockito.mock(MethodDescriptor.class);
- when(method.getParameterClasses())
- .thenReturn(new Class[]{String.class});
- when(method.getMethodName())
- .thenReturn("sayHello");
- String request = "request";
+ return result;
+ });
CountDownLatch latch = new CountDownLatch(11);
StreamObserver<Object> responseObserver = new StreamObserver<Object>()
{
@Override
@@ -232,8 +181,7 @@ class StubInvocationUtilTest {
latch.countDown();
}
};
- StreamObserver<Object> observer =
StubInvocationUtil.biOrClientStreamCall(invoker, method,
- responseObserver);
+ StreamObserver<Object> observer =
StubInvocationUtil.biOrClientStreamCall(invoker, method, responseObserver);
for (int i = 0; i < 10; i++) {
observer.onNext(request);
}
@@ -243,39 +191,15 @@ class StubInvocationUtilTest {
@Test
void serverStreamCall() throws InterruptedException {
- Invoker<IGreeter> invoker = Mockito.mock(Invoker.class);
- URL url = Mockito.mock(URL.class);
- ConsumerModel consumerModel = Mockito.mock(ConsumerModel.class);
- ServiceDescriptor serviceDescriptor =
Mockito.mock(ServiceDescriptor.class);
- when(consumerModel.getServiceModel()).thenReturn(serviceDescriptor);
- when(url.getServiceModel())
- .thenReturn(consumerModel);
- when(url.getServiceInterface())
- .thenReturn(IGreeter.class.getName());
- when(url.getProtocolServiceKey())
- .thenReturn(IGreeter.class.getName());
- when(invoker.getUrl())
- .thenReturn(url);
- when(invoker.getInterface())
- .thenReturn(IGreeter.class);
- Result result = Mockito.mock(Result.class);
- String response = "response";
- when(invoker.invoke(any(Invocation.class)))
- .then(invocationOnMock -> {
- Invocation invocation = (Invocation)
invocationOnMock.getArguments()[0];
- StreamObserver<Object> observer = (StreamObserver<Object>)
invocation.getArguments()[1];
- for (int i = 0; i < 10; i++) {
- observer.onNext(response);
- }
- observer.onCompleted();
- return result;
- });
- MethodDescriptor method = Mockito.mock(MethodDescriptor.class);
- when(method.getParameterClasses())
- .thenReturn(new Class[]{String.class});
- when(method.getMethodName())
- .thenReturn("sayHello");
- String request = "request";
+ when(invoker.invoke(any(Invocation.class))).then(invocationOnMock -> {
+ Invocation invocation = (Invocation)
invocationOnMock.getArguments()[0];
+ StreamObserver<Object> observer = (StreamObserver<Object>)
invocation.getArguments()[1];
+ for (int i = 0; i < 10; i++) {
+ observer.onNext(response);
+ }
+ observer.onCompleted();
+ return result;
+ });
CountDownLatch latch = new CountDownLatch(11);
StreamObserver<Object> responseObserver = new StreamObserver<Object>()
{
@Override