Search in sources :

Example 6 with ChannelBuffer

use of org.apache.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);
        Assertions.fail();
    } catch (IOException e) {
        Assertions.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);
    Assertions.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assertions.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assertions.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) IOException(java.io.IOException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ThriftCodecTest method testDecodeExceptionResponse.

@Test
public void testDecodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.class.getName());
    Channel channel = new MockedChannel(url);
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
    Request request = createRequest();
    DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
    TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId());
    TTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TApplicationException exception = new TApplicationException();
    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.class.getName());
    // path
    protocol.writeString(Demo.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    protocol.writeMessageBegin(message);
    exception.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare
    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));
    Object obj = codec.decode((Channel) null, bis);
    Assertions.assertNotNull(obj);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertTrue(response.getResult() instanceof AppResponse);
    AppResponse result = (AppResponse) response.getResult();
    Assertions.assertTrue(result.hasException());
    Assertions.assertTrue(result.getException() instanceof RpcException);
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture) TApplicationException(org.apache.thrift.TApplicationException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 8 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ThriftCodecTest method testDecodeReplyResponse.

@Test
public void testDecodeReplyResponse() 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);
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
    Request request = createRequest();
    DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
    TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId());
    Demo.echoString_result methodResult = new Demo.echoString_result();
    methodResult.success = "Hello, World!";
    TTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.Iface.class.getName());
    // path
    protocol.writeString(Demo.Iface.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    protocol.writeMessageBegin(message);
    methodResult.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare
    byte[] buf = new byte[4 + bos.size()];
    System.arraycopy(bos.toByteArray(), 0, buf, 4, bos.size());
    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);
    Object obj = codec.decode((Channel) null, bis);
    Assertions.assertNotNull(obj);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertEquals(request.getId(), response.getId());
    Assertions.assertTrue(response.getResult() instanceof AppResponse);
    AppResponse result = (AppResponse) response.getResult();
    Assertions.assertTrue(result.getValue() instanceof String);
    Assertions.assertEquals(methodResult.success, result.getValue());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 9 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeRequest.

@Test
public void testEncodeRequest() throws Exception {
    Request request = createRequest();
    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, output, request);
    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    TTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    // frame
    byte[] length = new byte[4];
    transport.read(length, 0, 4);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    // magic
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    // message length
    int messageLength = protocol.readI32();
    Assertions.assertEquals(messageLength + 4, bytes.length);
    // header length
    short headerLength = protocol.readI16();
    // version
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    // service name
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    // path
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    // dubbo request id
    Assertions.assertEquals(request.getId(), protocol.readI64());
    // test message header length
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Demo.echoString_args args = new Demo.echoString_args();
    args.read(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.CALL, message.type);
    Assertions.assertEquals("Hello, World!", args.getArg());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 10 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class GrizzlyCodecAdapter method handleWrite.

@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        // Do not need to close
        ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024);
        Object msg = context.getMessage();
        codec.encode(channel, channelBuffer, msg);
        GrizzlyChannel.removeChannelIfDisconnected(connection);
        Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
        buffer.put(channelBuffer.toByteBuffer());
        buffer.flip();
        buffer.allowBufferDispose(true);
        context.setMessage(buffer);
    } finally {
        GrizzlyChannel.removeChannelIfDisconnected(connection);
    }
    return context.getInvokeAction();
}
Also used : ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Buffer(org.glassfish.grizzly.Buffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)

Aggregations

ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)22 Channel (org.apache.dubbo.remoting.Channel)16 Test (org.junit.jupiter.api.Test)13 Request (org.apache.dubbo.remoting.exchange.Request)9 Response (org.apache.dubbo.remoting.exchange.Response)9 Demo (org.apache.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 (org.apache.dubbo.common.URL)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 TTransport (org.apache.thrift.transport.TTransport)3 DynamicChannelBuffer (org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)2 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 TApplicationException (org.apache.thrift.TApplicationException)2 Buffer (org.glassfish.grizzly.Buffer)2 ByteBuf (io.netty.buffer.ByteBuf)1