Search in sources :

Example 1 with MockedChannel

use of org.apache.dubbo.remoting.handler.MockedChannel 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 2 with MockedChannel

use of org.apache.dubbo.remoting.handler.MockedChannel 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)

Example 3 with MockedChannel

use of org.apache.dubbo.remoting.handler.MockedChannel 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 4 with MockedChannel

use of org.apache.dubbo.remoting.handler.MockedChannel 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)

Aggregations

Channel (org.apache.dubbo.remoting.Channel)4 Request (org.apache.dubbo.remoting.exchange.Request)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 Test (org.junit.jupiter.api.Test)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ExecutorService (java.util.concurrent.ExecutorService)2 TimeoutException (org.apache.dubbo.remoting.TimeoutException)2 ThreadlessExecutor (org.apache.dubbo.common.threadpool.ThreadlessExecutor)1 Disabled (org.junit.jupiter.api.Disabled)1