Skip to content

Commit

Permalink
Fix tri future (#11455)
Browse files Browse the repository at this point in the history
* fix tri return future

* fix ut

* fix npe

* fix sonar
  • Loading branch information
EarthChen authored Feb 6, 2023
1 parent c0b257a commit 0489702
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@

package org.apache.dubbo.rpc.protocol.tri;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.serialize.MultipleSerialization;
Expand All @@ -37,6 +29,14 @@

import com.google.protobuf.Message;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.apache.dubbo.common.constants.CommonConstants.$ECHO;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOBUF_MESSAGE_CLASS_NAME;
import static org.apache.dubbo.remoting.Constants.SERIALIZATION_KEY;
Expand Down Expand Up @@ -75,7 +75,7 @@ public ReflectionPackableMethod(MethodDescriptor method, URL url, String seriali
break;
case UNARY:
actualRequestTypes = method.getParameterClasses();
actualResponseType = method.getReturnClass();
actualResponseType = (Class<?>) method.getReturnTypes()[0];
break;
default:
throw new IllegalStateException("Can not reach here");
Expand Down Expand Up @@ -478,7 +478,7 @@ private static Class<?> getClassFromCache(String className, Map<String, Class<?>
if (clz == null) {
try {
clz = ClassUtils.forName(className);
} catch (Throwable e) {
} catch (Exception e) {
// To catch IllegalStateException, LinkageError, ClassNotFoundException
clz = expectedClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ private void doSendMessage(Object message) {

@Override
public final void onComplete() {
if (listener == null) {
// It will enter here when there is an error in the header
return;
}
listener.onComplete();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

import org.apache.dubbo.common.stream.StreamObserver;

import java.util.concurrent.CompletableFuture;

public interface DescriptorService {


CompletableFuture<String> unaryFuture();

void noParameterMethod();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.concurrent.CompletableFuture;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -35,6 +36,16 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

class ReflectionPackableMethodTest {


@Test
void testUnaryFuture() throws Exception {
Method method = DescriptorService.class.getMethod("unaryFuture");
MethodDescriptor descriptor = new ReflectionMethodDescriptor(method);
assertEquals(CompletableFuture.class, descriptor.getReturnClass());
assertEquals(String.class, descriptor.getReturnTypes()[0]);
}

@Test
void testMethodWithNoParameters() throws Exception {
Method method = DescriptorService.class.getMethod("noParameterMethod");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.MethodDescriptor.RpcType;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.model.ReflectionMethodDescriptor;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
Expand All @@ -46,8 +46,8 @@ void doStartCall() throws NoSuchMethodException {
Invoker<?> invoker = Mockito.mock(Invoker.class);
TripleServerStream serverStream = Mockito.mock(TripleServerStream.class);
ProviderModel providerModel = Mockito.mock(ProviderModel.class);
ReflectionMethodDescriptor methodDescriptor = Mockito.mock(
ReflectionMethodDescriptor.class);
Method method = DescriptorService.class.getMethod("sayHello", HelloReply.class);
MethodDescriptor methodDescriptor = new ReflectionMethodDescriptor(method);
URL url = Mockito.mock(URL.class);
when(invoker.getUrl())
.thenReturn(url);
Expand All @@ -68,19 +68,11 @@ void doStartCall() throws NoSuchMethodException {
}

ServiceDescriptor serviceDescriptor = Mockito.mock(ServiceDescriptor.class);
when(providerModel.getServiceModel())
.thenReturn(serviceDescriptor);
when(serviceDescriptor.getMethods(anyString()))
.thenReturn(Collections.singletonList(methodDescriptor));
when(methodDescriptor.getRpcType())
.thenReturn(RpcType.UNARY);
Method method = DescriptorService.class.getMethod("sayHello", HelloReply.class);
when(methodDescriptor.getMethod())
.thenReturn(method);
when(methodDescriptor.getParameterClasses())
.thenReturn(method.getParameterTypes());
when(methodDescriptor.getReturnClass())
.thenAnswer(invocation -> method.getReturnType());

when(providerModel.getServiceModel())
.thenReturn(serviceDescriptor);

ReflectionAbstractServerCall call2 = new ReflectionAbstractServerCall(invoker, serverStream,
new FrameworkModel(), "",
Expand Down

0 comments on commit 0489702

Please sign in to comment.