use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer 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);
Assert.assertEquals(person, obj.getResult());
// only decode necessary bytes
Assert.assertEquals(request.length, buffer.readerIndex());
}
use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class ExchangeCodecTest method test_Encode_Request.
@Test
public void test_Encode_Request() throws IOException {
ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
Channel channel = getCliendSideChannel(url);
Request request = new Request();
Person person = new Person();
request.setData(person);
codec.encode(channel, encodeBuffer, request);
// encode resault check need decode
byte[] data = new byte[encodeBuffer.writerIndex()];
encodeBuffer.readBytes(data);
ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
Request obj = (Request) codec.decode(channel, decodeBuffer);
Assert.assertEquals(request.isBroken(), obj.isBroken());
Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
Assert.assertEquals(person, obj.getData());
}
use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class ExchangeCodecTest method testMessageLengthExceedPayloadLimitWhenEncode.
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
Request request = new Request(1L);
request.setData("hello");
ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
try {
codec.encode(channel, encodeBuffer, request);
Assert.fail();
} catch (IOException e) {
Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
}
Response response = new Response(1L);
response.setResult("hello");
encodeBuffer = ChannelBuffers.dynamicBuffer(512);
channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
codec.encode(channel, encodeBuffer, response);
Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
Response receiveMessage = (Response) channel.getReceivedMessage();
Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class TelnetCodecTest method testDecode_assertEquals.
private void testDecode_assertEquals(AbstractMockChannel channel, Object request, Object expectret, Object channelReceive) throws IOException {
// init channel
if (channel == null) {
channel = getServerSideChannel(url);
}
byte[] buf = objectToByte(request);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(buf);
// decode
Object obj = codec.decode(channel, buffer);
Assert.assertEquals(expectret, obj);
Assert.assertEquals(channelReceive, channel.getReceivedMessage());
}
use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.
the class TelnetCodecTest method testDecode_WithExitByte.
private void testDecode_WithExitByte(byte[] exitbytes, boolean isChannelClose) throws IOException {
// init channel
Channel channel = getServerSideChannel(url);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(exitbytes);
// decode
codec.decode(channel, buffer);
Assert.assertEquals(isChannelClose, channel.isClosed());
}
Aggregations