Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update StubInvocationUtilTest.java #13174

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand All @@ -40,110 +41,83 @@
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>() {
Expand All @@ -168,54 +142,29 @@ public void onCompleted() {

@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
Expand All @@ -232,8 +181,7 @@ public void onCompleted() {
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);
}
Expand All @@ -243,39 +191,15 @@ public void onCompleted() {

@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
Expand Down
Loading