Search in sources :

Example 11 with ChannelBuffer

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());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 12 with ChannelBuffer

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());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 13 with ChannelBuffer

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: "));
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) IOException(java.io.IOException) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 14 with ChannelBuffer

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());
}
Also used : ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 15 with ChannelBuffer

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());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Aggregations

ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)20 Channel (com.alibaba.dubbo.remoting.Channel)15 Test (org.junit.Test)12 Request (com.alibaba.dubbo.remoting.exchange.Request)8 Response (com.alibaba.dubbo.remoting.exchange.Response)8 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)5 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)5 TMessage (org.apache.thrift.protocol.TMessage)5 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)5 URL (com.alibaba.dubbo.common.URL)4 RpcResult (com.alibaba.dubbo.rpc.RpcResult)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 TTransport (org.apache.thrift.transport.TTransport)3 DynamicChannelBuffer (com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer)2 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 IOException (java.io.IOException)2 TApplicationException (org.apache.thrift.TApplicationException)2 Buffer (org.glassfish.grizzly.Buffer)2 UnsafeByteArrayOutputStream (com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream)1