Search in sources :

Example 31 with Request

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

the class ThriftCodecTest method testEncodeExceptionResponse.

@Test
public void testEncodeExceptionResponse() 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();
    String exceptionMessage = "failed";
    appResponse.setException(new RuntimeException(exceptionMessage));
    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.put(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.EXCEPTION, message.type);
    Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    TApplicationException exception = TApplicationException.readFrom(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(exceptionMessage, exception.getMessage());
}
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) TApplicationException(org.apache.thrift.TApplicationException) 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 32 with Request

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

the class DubboTelnetDecodeTest method createDubboByteBuf.

private ByteBuf createDubboByteBuf() throws IOException {
    Request request = new Request();
    RpcInvocation rpcInvocation = new RpcInvocation();
    rpcInvocation.setMethodName("sayHello");
    rpcInvocation.setParameterTypes(new Class[] { String.class });
    rpcInvocation.setParameterTypesDesc(ReflectUtils.getDesc(new Class[] { String.class }));
    rpcInvocation.setArguments(new String[] { "dubbo" });
    rpcInvocation.setAttachment("path", DemoService.class.getName());
    rpcInvocation.setAttachment("interface", DemoService.class.getName());
    rpcInvocation.setAttachment("version", "0.0.0");
    request.setData(rpcInvocation);
    request.setVersion("2.0.2");
    ByteBuf dubboByteBuf = Unpooled.buffer();
    ChannelBuffer buffer = new NettyBackedChannelBuffer(dubboByteBuf);
    DubboCodec dubboCodec = new DubboCodec();
    dubboCodec.encode(new MockChannel(), buffer, request);
    return dubboByteBuf;
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) DecodeableRpcInvocation(org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation) DubboCodec(org.apache.dubbo.rpc.protocol.dubbo.DubboCodec) NettyBackedChannelBuffer(org.apache.dubbo.remoting.transport.netty4.NettyBackedChannelBuffer) Request(org.apache.dubbo.remoting.exchange.Request) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) ByteBuf(io.netty.buffer.ByteBuf) NettyBackedChannelBuffer(org.apache.dubbo.remoting.transport.netty4.NettyBackedChannelBuffer) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 33 with Request

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

the class HeaderExchangeChannelTest method requestTest02.

@Test
public void requestTest02() throws RemotingException {
    Channel channel = Mockito.mock(MockChannel.class);
    header = new HeaderExchangeChannel(channel);
    when(channel.getUrl()).thenReturn(url);
    Object requestob = new Object();
    header.request(requestob);
    ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel, times(1)).send(argumentCaptor.capture());
    Assertions.assertEquals(argumentCaptor.getValue().getData(), requestob);
}
Also used : Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) Test(org.junit.jupiter.api.Test)

Example 34 with Request

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

the class DefaultFutureTest method interruptSend.

/**
 * for example, it will print like this:
 *before a future is create , time is : 2021-01-22 10:55:03
 * null
 * after a future is timeout , time is : 2021-01-22 10:55:05
 */
@Test
public void interruptSend() throws Exception {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    System.out.println("before a future is create , time is : " + LocalDateTime.now().format(formatter));
    // timeout after 1 seconds.
    Channel channel = new MockedChannel();
    int channelId = 10;
    Request request = new Request(channelId);
    ExecutorService sharedExecutor = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().createExecutorIfAbsent(URL.valueOf("dubbo://127.0.0.1:23456"));
    ThreadlessExecutor executor = new ThreadlessExecutor(sharedExecutor);
    DefaultFuture f = DefaultFuture.newFuture(channel, request, 1000, executor);
    // mark the future is sent
    DefaultFuture.sent(channel, request);
    // get operate will throw a interrupted exception, because the thread is interrupted.
    try {
        new InterruptThread(Thread.currentThread()).start();
        executor.waitAndDrain();
        f.get();
    } catch (Exception e) {
        Assertions.assertTrue(e instanceof InterruptedException, "catch exception is not interrupted exception!");
        System.out.println(e.getMessage());
    }
    // waiting timeout check task finished
    Thread.sleep(1500);
    System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
    DefaultFuture future = DefaultFuture.getFuture(channelId);
    // waiting future should be removed by time out check task
    Assertions.assertNull(future);
}
Also used : ThreadlessExecutor(org.apache.dubbo.common.threadpool.ThreadlessExecutor) MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) ExecutorService(java.util.concurrent.ExecutorService) DateTimeFormatter(java.time.format.DateTimeFormatter) TimeoutException(org.apache.dubbo.remoting.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 35 with Request

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

the class DefaultFutureTest method defaultFuture.

/**
 * mock a default future
 */
private DefaultFuture defaultFuture(int timeout) {
    Channel channel = new MockedChannel();
    Request request = new Request(index.getAndIncrement());
    return DefaultFuture.newFuture(channel, request, timeout, null);
}
Also used : MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) Channel(org.apache.dubbo.remoting.Channel) 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