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());
}
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());
}
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);
}
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;
}
}
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;
}
Aggregations