Search in sources :

Example 6 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Return_Response_Error.

// The status input has a problem, and the read information is wrong when the serialization is serialized.
@Test
public void test_Decode_Return_Response_Error() throws IOException {
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 2, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    String errorString = "encode request data error ";
    byte[] request = getRequestBytes(errorString, header);
    Response obj = (Response) decode(request);
    Assertions.assertEquals(90, obj.getStatus());
    Assertions.assertEquals(errorString, obj.getErrorMessage());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Test(org.junit.jupiter.api.Test)

Example 7 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Error_Response.

@Test
public void test_Encode_Error_Response() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    Channel channel = getCliendSideChannel(url);
    Response response = new Response();
    response.setHeartbeat(true);
    response.setId(1001L);
    response.setStatus((byte) 10);
    response.setVersion("11");
    String badString = "bad";
    response.setErrorMessage(badString);
    Person person = new Person();
    response.setResult(person);
    codec.encode(channel, encodeBuffer, response);
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    // encode resault check need decode
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Response obj = (Response) codec.decode(channel, decodeBuffer);
    Assertions.assertEquals(response.getId(), obj.getId());
    Assertions.assertEquals(response.getStatus(), obj.getStatus());
    Assertions.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
    Assertions.assertEquals(badString, obj.getErrorMessage());
    Assertions.assertNull(obj.getResult());
// Assertions.assertEquals(response.getProtocolVersion(), obj.getVersion());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with Response

use of org.apache.dubbo.remoting.exchange.Response 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 9 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class HeaderExchangeHandlerTest method test_received_request_twoway.

@Test
public void test_received_request_twoway() throws RemotingException {
    final Person requestdata = new Person("charles");
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(requestdata);
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response) message;
            Assertions.assertEquals(request.getId(), res.getId());
            Assertions.assertEquals(request.getVersion(), res.getVersion());
            Assertions.assertEquals(Response.OK, res.getStatus());
            Assertions.assertEquals(requestdata, res.getResult());
            Assertions.assertNull(res.getErrorMessage());
            count.incrementAndGet();
        }
    };
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        @Override
        public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
            return CompletableFuture.completedFuture(request);
        }

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            Assertions.fail();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
    Assertions.assertEquals(1, count.get());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) ExchangeHandler(org.apache.dubbo.remoting.exchange.ExchangeHandler) Test(org.junit.jupiter.api.Test)

Example 10 with Response

use of org.apache.dubbo.remoting.exchange.Response in project dubbo by alibaba.

the class DubboCodec method decodeBody.

@Override
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
    byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
    // get request id.
    long id = Bytes.bytes2long(header, 4);
    if ((flag & FLAG_REQUEST) == 0) {
        // decode response.
        Response res = new Response(id);
        if ((flag & FLAG_EVENT) != 0) {
            res.setEvent(true);
        }
        // get status.
        byte status = header[3];
        res.setStatus(status);
        try {
            if (status == Response.OK) {
                Object data;
                if (res.isEvent()) {
                    byte[] eventPayload = CodecSupport.getPayload(is);
                    if (CodecSupport.isHeartBeat(eventPayload, proto)) {
                        // heart beat response data is always null;
                        data = null;
                    } else {
                        ObjectInput in = CodecSupport.deserialize(channel.getUrl(), new ByteArrayInputStream(eventPayload), proto);
                        data = decodeEventData(channel, in, eventPayload);
                    }
                } else {
                    DecodeableRpcResult result;
                    if (channel.getUrl().getParameter(DECODE_IN_IO_THREAD_KEY, DEFAULT_DECODE_IN_IO_THREAD)) {
                        result = new DecodeableRpcResult(channel, res, is, (Invocation) getRequestData(id), proto);
                        result.decode();
                    } else {
                        result = new DecodeableRpcResult(channel, res, new UnsafeByteArrayInputStream(readMessageData(is)), (Invocation) getRequestData(id), proto);
                    }
                    data = result;
                }
                res.setResult(data);
            } else {
                ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
                res.setErrorMessage(in.readUTF());
            }
        } catch (Throwable t) {
            if (log.isWarnEnabled()) {
                log.warn("Decode response failed: " + t.getMessage(), t);
            }
            res.setStatus(Response.CLIENT_ERROR);
            res.setErrorMessage(StringUtils.toString(t));
        }
        return res;
    } else {
        // decode request.
        Request req = new Request(id);
        req.setVersion(Version.getProtocolVersion());
        req.setTwoWay((flag & FLAG_TWOWAY) != 0);
        if ((flag & FLAG_EVENT) != 0) {
            req.setEvent(true);
        }
        try {
            Object data;
            if (req.isEvent()) {
                byte[] eventPayload = CodecSupport.getPayload(is);
                if (CodecSupport.isHeartBeat(eventPayload, proto)) {
                    // heart beat response data is always null;
                    data = null;
                } else {
                    ObjectInput in = CodecSupport.deserialize(channel.getUrl(), new ByteArrayInputStream(eventPayload), proto);
                    data = decodeEventData(channel, in, eventPayload);
                }
            } else {
                DecodeableRpcInvocation inv;
                if (channel.getUrl().getParameter(DECODE_IN_IO_THREAD_KEY, DEFAULT_DECODE_IN_IO_THREAD)) {
                    inv = new DecodeableRpcInvocation(channel, req, is, proto);
                    inv.decode();
                } else {
                    inv = new DecodeableRpcInvocation(channel, req, new UnsafeByteArrayInputStream(readMessageData(is)), proto);
                }
                data = inv;
            }
            req.setData(data);
        } catch (Throwable t) {
            if (log.isWarnEnabled()) {
                log.warn("Decode request failed: " + t.getMessage(), t);
            }
            // bad request
            req.setBroken(true);
            req.setData(t);
        }
        return req;
    }
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Request(org.apache.dubbo.remoting.exchange.Request) UnsafeByteArrayInputStream(org.apache.dubbo.common.io.UnsafeByteArrayInputStream) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsafeByteArrayInputStream(org.apache.dubbo.common.io.UnsafeByteArrayInputStream) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput)

Aggregations

Response (org.apache.dubbo.remoting.exchange.Response)39 Request (org.apache.dubbo.remoting.exchange.Request)21 Test (org.junit.jupiter.api.Test)20 Channel (org.apache.dubbo.remoting.Channel)12 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 IOException (java.io.IOException)6 RemotingException (org.apache.dubbo.remoting.RemotingException)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 TMessage (org.apache.thrift.protocol.TMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)3 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)3 TApplicationException (org.apache.thrift.TApplicationException)3 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)2