Search in sources :

Example 1 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class ProtostuffSerializerTest method testSofaResponse.

@Test
public void testSofaResponse() throws Exception {
    SofaResponse response = new SofaResponse();
    response.setAppResponse("1233");
    AbstractByteBuf data = serializer.encode(response, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaResponse.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaResponse(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // success response
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    response = new SofaResponse();
    final ExampleObj exampleObj = new ExampleObj();
    exampleObj.setName("result");
    response.setAppResponse(exampleObj);
    data = serializer.encode(response, null);
    SofaResponse newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse());
    Assert.assertEquals("result", ((ExampleObj) newResponse.getAppResponse()).getName());
    // null response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    newResponse = new SofaResponse();
    serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertNotNull(newResponse.getAppResponse());
    Assert.assertNull(((ExampleObj) newResponse.getAppResponse()).getName());
    // error response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put(RemotingConstants.HEAD_RESPONSE_ERROR, "true");
    response = new SofaResponse();
    response.setErrorMsg("1233");
    data = serializer.encode(response, null);
    newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertTrue(newResponse.isError());
    Assert.assertEquals(response.getErrorMsg(), newResponse.getErrorMsg());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 2 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class ProtostuffSerializerTest method testSofaRequest.

@Test
public void testSofaRequest() throws Exception {
    SofaRequest request = buildRequest();
    AbstractByteBuf data = serializer.encode(request, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaRequest.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaRequest(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put("unkown", "yes");
    SofaRequest newRequest = new SofaRequest();
    serializer.decode(data, newRequest, head);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals("xxxx", ((ExampleObj) newRequest.getMethodArgs()[0]).getName());
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    Assert.assertEquals(newRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME), request.getRequestProp(RemotingConstants.RPC_TRACE_NAME));
    // null request
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtostuffService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    newRequest = new SofaRequest();
    serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newRequest, head);
    Assert.assertNull(((ExampleObj) newRequest.getMethodArgs()[0]).getName());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 3 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class SofaResponseTest method getResponseProp.

@Test
public void getResponseProp() {
    SofaResponse response = new SofaResponse();
    response.setErrorMsg(null);
    Assert.assertFalse(response.isError());
    response = new SofaResponse();
    response.setErrorMsg("1233");
    Assert.assertTrue(response.isError());
    Assert.assertEquals("1233", response.getErrorMsg());
    response = new SofaResponse();
    response.setAppResponse(new RuntimeException("1233"));
    Assert.assertTrue(response.getAppResponse() instanceof RuntimeException);
    Assert.assertFalse(response.isError());
    response = new SofaResponse();
    response.setAppResponse("1233");
    Assert.assertFalse(response.isError());
    Assert.assertEquals("1233", response.getAppResponse());
    response.setSerializeType((byte) 11);
    response.setData(new ByteArrayWrapperByteBuf(new byte[] { 1, 2, 3 }));
    Assert.assertTrue(response.getSerializeType() == 11);
    Assert.assertTrue(response.getData().array().length == 3);
    Map<String, String> map = response.getResponseProps();
    Assert.assertTrue(map == null);
    response.addResponseProp("1", "1");
    map = response.getResponseProps();
    Assert.assertTrue(map.size() == 1);
    response.addResponseProp(null, "1");
    Assert.assertTrue(map.size() == 1);
    response.addResponseProp("1", null);
    Assert.assertTrue(map.size() == 1);
    response.removeResponseProp(null);
    Assert.assertTrue(map.size() == 1);
    response.removeResponseProp("1");
    Assert.assertTrue(map.size() == 0);
    Assert.assertNull(response.getResponseProp("1"));
    response.setResponseProps(Collections.singletonMap("1", "1"));
    Assert.assertTrue(response.getResponseProps().size() == 1);
}
Also used : ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) Test(org.junit.Test)

Example 4 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class TripleClientInvoker method invoke.

@Override
public SofaResponse invoke(SofaRequest sofaRequest, int timeout) throws Exception {
    if (!useGeneric) {
        SofaResponse sofaResponse = new SofaResponse();
        Object stub = sofaStub.invoke(null, channel, buildCustomCallOptions(sofaRequest, timeout), timeout);
        final Method method = sofaRequest.getMethod();
        Object appResponse = method.invoke(stub, sofaRequest.getMethodArgs()[0]);
        sofaResponse.setAppResponse(appResponse);
        return sofaResponse;
    } else {
        String serviceName = sofaRequest.getInterfaceName();
        String methodName = sofaRequest.getMethodName();
        MethodDescriptor.Marshaller<?> requestMarshaller = null;
        MethodDescriptor.Marshaller<?> responseMarshaller = null;
        requestMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Request.getDefaultInstance());
        responseMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Response.getDefaultInstance());
        String fullMethodName = generateFullMethodName(serviceName, methodName);
        MethodDescriptor methodDescriptor = io.grpc.MethodDescriptor.newBuilder().setType(io.grpc.MethodDescriptor.MethodType.UNARY).setFullMethodName(fullMethodName).setSampledToLocalTracing(true).setRequestMarshaller((MethodDescriptor.Marshaller<Object>) requestMarshaller).setResponseMarshaller((MethodDescriptor.Marshaller<Object>) responseMarshaller).build();
        Request request = getRequest(sofaRequest, serialization, serializer);
        Response response = (Response) ClientCalls.blockingUnaryCall(channel, methodDescriptor, buildCustomCallOptions(sofaRequest, timeout), request);
        SofaResponse sofaResponse = new SofaResponse();
        byte[] responseDate = response.getData().toByteArray();
        Class returnType = sofaRequest.getMethod().getReturnType();
        if (returnType != void.class) {
            if (responseDate != null && responseDate.length > 0) {
                Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
                Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
                sofaResponse.setAppResponse(appResponse);
            }
        }
        return sofaResponse;
    }
}
Also used : Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Method(java.lang.reflect.Method) ByteString(com.google.protobuf.ByteString) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) MethodDescriptor(io.grpc.MethodDescriptor) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 5 with ByteArrayWrapperByteBuf

use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.

the class GenericServiceImplTest method getReturnValue.

private Object getReturnValue(Method method) {
    Object appResponse = null;
    Response response = responseObserver.getValue();
    byte[] responseDate = response.getData().toByteArray();
    Class returnType = method.getReturnType();
    if (returnType != void.class) {
        if (responseDate != null && responseDate.length > 0) {
            Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
            appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
        }
    }
    return appResponse;
}
Also used : Response(triple.Response) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaHessianSerializer(com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Aggregations

ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)16 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)10 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)8 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)8 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)6 Serializer (com.alipay.sofa.rpc.codec.Serializer)4 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 SerializationException (com.alipay.remoting.exception.SerializationException)2 DemoRequest (com.alipay.sofa.rpc.codec.jackson.model.DemoRequest)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Response (triple.Response)2 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)1 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)1 DemoRequest2 (com.alipay.sofa.rpc.codec.jackson.model.DemoRequest2)1 DemoResponse (com.alipay.sofa.rpc.codec.msgpack.model.DemoResponse)1