use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.
the class ThriftCodecTest method testEncodeExceptionResponse.
@Test
public void testEncodeExceptionResponse() throws Exception {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
Channel channel = new MockedChannel(url);
Request request = createRequest();
AppResponse appResponse = new AppResponse();
String exceptionMessage = "failed";
appResponse.setException(new RuntimeException(exceptionMessage));
Response response = new Response();
response.setResult(appResponse);
response.setId(request.getId());
ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
ThriftCodec.CACHED_REQUEST.put(request.getId(), rd);
codec.encode(channel, bos, response);
byte[] buf = new byte[bos.writerIndex() - 4];
System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
if (bis.markSupported()) {
bis.mark(0);
}
TIOStreamTransport transport = new TIOStreamTransport(bis);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
int headerLength = protocol.readI16();
Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
Assertions.assertEquals(request.getId(), protocol.readI64());
if (bis.markSupported()) {
bis.reset();
bis.skip(headerLength);
}
TMessage message = protocol.readMessageBegin();
Assertions.assertEquals("echoString", message.name);
Assertions.assertEquals(TMessageType.EXCEPTION, message.type);
Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
TApplicationException exception = TApplicationException.readFrom(protocol);
protocol.readMessageEnd();
Assertions.assertEquals(exceptionMessage, exception.getMessage());
}
use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.
the class ExchangeCodecTest method test_Header_Response_Heartbeat.
@Test
public void test_Header_Response_Heartbeat() throws IOException {
// 00000010-response/oneway/hearbeat=true |20-stats=ok|id=0|length=0
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x02, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Person person = new Person();
byte[] request = getRequestBytes(person, header);
Response obj = (Response) decode(request);
Assertions.assertEquals(20, obj.getStatus());
Assertions.assertEquals(person, obj.getResult());
System.out.println(obj);
}
use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Return_Response_Person.
@Test
public void test_Decode_Return_Response_Person() throws IOException {
// 00000010-response/oneway/hearbeat=false/hessian |20-stats=ok|id=0|length=0
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Person person = new Person();
byte[] request = getRequestBytes(person, header);
Response obj = (Response) decode(request);
Assertions.assertEquals(20, obj.getStatus());
Assertions.assertEquals(person, obj.getResult());
System.out.println(obj);
}
use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Check_Payload.
@Test
public void test_Decode_Check_Payload() throws IOException {
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
byte[] request = assemblyDataProtocol(header);
try {
Channel channel = getServerSideChannel(url);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request);
Object obj = codec.decode(channel, buffer);
Assertions.assertTrue(obj instanceof Response);
Assertions.assertTrue(((Response) obj).getErrorMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[] { 1, 1, 1, 1 })));
} catch (IOException expected) {
Assertions.assertTrue(expected.getMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[] { 1, 1, 1, 1 })));
}
}
use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Error_Length.
@Test
public void test_Decode_Error_Length() throws IOException {
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x02, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Person person = new Person();
byte[] request = getRequestBytes(person, header);
Channel channel = getServerSideChannel(url);
byte[] baddata = new byte[] { 1, 2 };
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata));
Response obj = (Response) codec.decode(channel, buffer);
Assertions.assertEquals(person, obj.getResult());
// only decode necessary bytes
Assertions.assertEquals(request.length, buffer.readerIndex());
}
Aggregations