Search in sources :

Example 36 with Channel

use of org.apache.dubbo.remoting.Channel 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 37 with Channel

use of org.apache.dubbo.remoting.Channel 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 38 with Channel

use of org.apache.dubbo.remoting.Channel 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 39 with Channel

use of org.apache.dubbo.remoting.Channel 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 40 with Channel

use of org.apache.dubbo.remoting.Channel 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)53 Test (org.junit.jupiter.api.Test)32 Request (org.apache.dubbo.remoting.exchange.Request)18 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)16 Response (org.apache.dubbo.remoting.exchange.Response)12 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)9 URL (org.apache.dubbo.common.URL)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 RemotingException (org.apache.dubbo.remoting.RemotingException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TMessage (org.apache.thrift.protocol.TMessage)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 IOException (java.io.IOException)3