Search in sources :

Example 36 with Request

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

the class DefaultFutureTest method testClose.

@Test
public void testClose() throws Exception {
    Channel channel = new MockedChannel();
    Request request = new Request(123);
    ExecutorService executor = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().createExecutorIfAbsent(URL.valueOf("dubbo://127.0.0.1:23456"));
    DefaultFuture.newFuture(channel, request, 1000, executor);
    DefaultFuture.closeChannel(channel);
    Assertions.assertFalse(executor.isTerminated());
}
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) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 37 with Request

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

the class DefaultFutureTest method timeoutSend.

/**
 * for example, it will print like this:
 * before a future is create , time is : 2018-06-21 15:11:31
 * after a future is timeout , time is : 2018-06-21 15:11:36
 * <p>
 * The exception info print like:
 * Waiting server-side response timeout by scan timer.
 * start time: 2018-06-21 15:12:38.337, end time: 2018-06-21 15:12:43.354...
 */
@Test
@Disabled
public void timeoutSend() 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 5 seconds.
    Channel channel = new MockedChannel();
    Request request = new Request(10);
    DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null);
    // mark the future is sent
    DefaultFuture.sent(channel, request);
    while (!f.isDone()) {
        // spin
        Thread.sleep(100);
    }
    System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
    // get operate will throw a timeout exception, because the future is timeout.
    try {
        f.get();
    } catch (Exception e) {
        Assertions.assertTrue(e.getCause() instanceof TimeoutException, "catch exception is not timeout exception!");
        System.out.println(e.getMessage());
    }
}
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) DateTimeFormatter(java.time.format.DateTimeFormatter) TimeoutException(org.apache.dubbo.remoting.TimeoutException) TimeoutException(org.apache.dubbo.remoting.TimeoutException) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 38 with Request

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

the class HeartBeatTaskTest method testHeartBeat.

@Test
public void testHeartBeat() throws Exception {
    long now = System.currentTimeMillis();
    url = url.addParameter(DUBBO_VERSION_KEY, "2.1.1");
    channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now);
    channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now);
    heartbeatTimer.newTimeout(heartbeatTimerTask, 250, TimeUnit.MILLISECONDS);
    Thread.sleep(2000L);
    List<Object> objects = channel.getSentObjects();
    Assertions.assertTrue(objects.size() > 0);
    Object obj = objects.get(0);
    Assertions.assertTrue(obj instanceof Request);
    Request request = (Request) obj;
    Assertions.assertTrue(request.isHeartbeat());
}
Also used : Request(org.apache.dubbo.remoting.exchange.Request) Test(org.junit.jupiter.api.Test)

Example 39 with Request

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

the class ExchangeCodecTest method testMessageLengthGreaterThanMessageActualLength.

@Test
public void testMessageLengthGreaterThanMessageActualLength() throws Exception {
    Channel channel = getCliendSideChannel(url);
    Request request = new Request(1L);
    request.setVersion(Version.getProtocolVersion());
    Date date = new Date();
    request.setData(date);
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, encodeBuffer, request);
    byte[] bytes = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(bytes);
    int len = Bytes.bytes2int(bytes, 12);
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    out.write(bytes, 0, 12);
    /*
         * The fill length can not be less than 256, because by default, hessian reads 256 bytes from the stream each time.
         * Refer Hessian2Input.readBuffer for more details
         */
    int padding = 512;
    out.write(Bytes.int2bytes(len + padding));
    out.write(bytes, 16, bytes.length - 16);
    for (int i = 0; i < padding; i++) {
        out.write(1);
    }
    out.write(bytes);
    /* request|1111...|request */
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(out.toByteArray());
    Request decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assertions.assertEquals(date, decodedRequest.getData());
    Assertions.assertEquals(bytes.length + padding, decodeBuffer.readerIndex());
    decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assertions.assertEquals(date, decodedRequest.getData());
}
Also used : Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(org.apache.dubbo.common.io.UnsafeByteArrayOutputStream) Date(java.util.Date) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 40 with Request

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

the class ExchangeCodecTest method test_Decode_Return_Request_Heartbeat_Object.

@Test
public void test_Decode_Return_Request_Heartbeat_Object() throws IOException {
    // |10011111|20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xe2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] request = getRequestBytes(null, header);
    Request obj = (Request) decode(request);
    Assertions.assertNull(obj.getData());
    Assertions.assertTrue(obj.isTwoWay());
    Assertions.assertTrue(obj.isHeartbeat());
    Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion());
    System.out.println(obj);
}
Also used : Request(org.apache.dubbo.remoting.exchange.Request) Test(org.junit.jupiter.api.Test)

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