Search in sources :

Example 26 with Request

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

the class HeaderExchangeHandlerTest method test_received_request_event_other_discard.

@Test
public void test_received_request_event_other_discard() throws RemotingException {
    final Request request = new Request();
    request.setTwoWay(true);
    request.setEvent("my event");
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Assertions.fail();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler() {

        @Override
        public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
            Assertions.fail();
            throw new RemotingException(channel, "");
        }

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            Assertions.fail();
            throw new RemotingException(channel, "");
        }
    });
    hexhandler.received(mchannel, request);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Test(org.junit.jupiter.api.Test)

Example 27 with Request

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

the class DeprecatedExchangeCodec method getRequestData.

protected Object getRequestData(long id) {
    DefaultFuture future = DefaultFuture.getFuture(id);
    if (future == null)
        return null;
    Request req = future.getRequest();
    if (req == null)
        return null;
    return req.getData();
}
Also used : Request(org.apache.dubbo.remoting.exchange.Request) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture)

Example 28 with Request

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

the class ThriftCodecTest method testEncodeReplyResponse.

@Test
public void testEncodeReplyResponse() 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();
    appResponse.setValue("Hello, World!");
    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.putIfAbsent(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.REPLY, message.type);
    // Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    Demo.echoString_result result = new Demo.echoString_result();
    result.read(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(appResponse.getValue(), result.getSuccess());
}
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) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 29 with Request

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

the class ThriftCodecTest method testDecodeRequest.

@Test
public void testDecodeRequest() throws Exception {
    Request request = createRequest();
    // encode
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);
    TIOStreamTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    int messageLength, headerLength;
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(((RpcInvocation) request.getData()).getAttachment(INTERFACE_KEY));
    protocol.writeString(((RpcInvocation) request.getData()).getAttachment(PATH_KEY));
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    Demo.echoString_args args = new Demo.echoString_args();
    args.setArg("Hell, World!");
    TMessage message = new TMessage("echoString", TMessageType.CALL, ThriftCodec.getSeqId());
    protocol.writeMessageBegin(message);
    args.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    Object obj = codec.decode((Channel) null, ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray())));
    Assertions.assertTrue(obj instanceof Request);
    obj = ((Request) obj).getData();
    Assertions.assertTrue(obj instanceof RpcInvocation);
    RpcInvocation invocation = (RpcInvocation) obj;
    Assertions.assertEquals("echoString", invocation.getMethodName());
    Assertions.assertArrayEquals(new Class[] { String.class }, invocation.getParameterTypes());
    Assertions.assertArrayEquals(new Object[] { args.getArg() }, invocation.getArguments());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Demo(org.apache.dubbo.rpc.gen.thrift.Demo) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) Test(org.junit.jupiter.api.Test)

Example 30 with Request

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

the class ThriftCodecTest method createRequest.

private Request createRequest() {
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("echoString");
    invocation.setArguments(new Object[] { "Hello, World!" });
    invocation.setParameterTypes(new Class<?>[] { String.class });
    invocation.setAttachment(INTERFACE_KEY, Demo.Iface.class.getName());
    invocation.setAttachment(PATH_KEY, Demo.Iface.class.getName());
    Request request = new Request(1L);
    request.setData(invocation);
    return request;
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Request(org.apache.dubbo.remoting.exchange.Request)

Aggregations

Request (org.apache.dubbo.remoting.exchange.Request)51 Test (org.junit.jupiter.api.Test)30 Response (org.apache.dubbo.remoting.exchange.Response)21 Channel (org.apache.dubbo.remoting.Channel)18 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)10 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)9 RemotingException (org.apache.dubbo.remoting.RemotingException)7 TMessage (org.apache.thrift.protocol.TMessage)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 AppResponse (org.apache.dubbo.rpc.AppResponse)6 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)5 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 URL (org.apache.dubbo.common.URL)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 IOException (java.io.IOException)3