Search in sources :

Example 11 with ExchangeChannel

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

the class ReplierDispatcherTest method clientExchangeInfo.

void clientExchangeInfo(int port) throws Exception {
    ExchangeChannel client = Exchangers.connect(URL.valueOf("exchange://localhost:" + port + "?" + CommonConstants.TIMEOUT_KEY + "=5000"));
    clients.put(Thread.currentThread().getName(), client);
    MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class<?>[] { int.class, int.class }, new Object[] { 55, 25 })).get();
    Assertions.assertEquals(result.getResult(), 80);
    for (int i = 0; i < 100; i++) {
        client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class<?>[] { String.class }, new Object[] { "qianlei" + i }));
    }
    for (int i = 0; i < 100; i++) {
        CompletableFuture<Object> future = client.request(new Data());
        Assertions.assertEquals(future.get().toString(), "hello world");
    }
}
Also used : ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel)

Example 12 with ExchangeChannel

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

the class HeaderExchangeHandlerTest method test_received_request_twoway_error_reply.

@Test
public void test_received_request_twoway_error_reply() throws RemotingException {
    final Person requestdata = new Person("charles");
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(requestdata);
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response) message;
            Assertions.assertEquals(request.getId(), res.getId());
            Assertions.assertEquals(request.getVersion(), res.getVersion());
            Assertions.assertEquals(Response.SERVICE_ERROR, res.getStatus());
            Assertions.assertNull(res.getResult());
            Assertions.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
            count.incrementAndGet();
        }
    };
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        @Override
        public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
            throw new BizException();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
    Assertions.assertEquals(1, count.get());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) ExchangeHandler(org.apache.dubbo.remoting.exchange.ExchangeHandler) Test(org.junit.jupiter.api.Test)

Example 13 with ExchangeChannel

use of org.apache.dubbo.remoting.exchange.ExchangeChannel 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 14 with ExchangeChannel

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

the class DubboTelnetDecodeTest method testTelnetTelnetDecoded.

/**
 * NOTE: This test case actually will fail, but the probability of this case is very small,
 * and users should use telnet in new QOS port(default port is 22222) since dubbo 2.5.8,
 * so we could ignore this problem.
 *
 * <p>
 * telnet and telnet request
 *
 * <p>
 * First ByteBuf (firstByteBuf):
 * +--------------------------------------------------+
 * |               telnet(incomplete)                 |
 * +--------------------------------------------------+
 * <p>
 *
 * Second ByteBuf (secondByteBuf):
 * +--------------------------------------------------+
 * |  telnet(the remaining)   |   telnet(complete)    |
 * +--------------------------------------------------+
 *
 * @throws InterruptedException
 */
@Disabled
@Test
public void testTelnetTelnetDecoded() throws InterruptedException {
    ByteBuf firstByteBuf = Unpooled.wrappedBuffer("ls\r".getBytes());
    ByteBuf secondByteBuf = Unpooled.wrappedBuffer("\nls\r\n".getBytes());
    EmbeddedChannel ch = null;
    try {
        Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo");
        URL url = new URL("dubbo", "localhost", 22226);
        NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler());
        MockHandler mockHandler = new MockHandler((msg) -> {
            if (checkTelnetDecoded(msg)) {
                telnetTelnet.incrementAndGet();
            }
        }, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {

            @Override
            public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(firstByteBuf);
        ch.writeInbound(secondByteBuf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(2, telnetTelnet.get());
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) NettyCodecAdapter(org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter) Codec2(org.apache.dubbo.remoting.Codec2) MultiMessageHandler(org.apache.dubbo.remoting.transport.MultiMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ByteBuf(io.netty.buffer.ByteBuf) URL(org.apache.dubbo.common.URL) IOException(java.io.IOException) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) DecodeHandler(org.apache.dubbo.remoting.transport.DecodeHandler) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 15 with ExchangeChannel

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

the class DubboTelnetDecodeTest method testTelnetDubboDecoded.

/**
 * telnet and dubbo request
 *
 * <p>
 * First ByteBuf:
 * +--------------------------------------------------+
 * |               telnet(incomplete)                 |
 * +--------------------------------------------------+
 * <p>
 *
 * Second ByteBuf:
 * +--------------------------++----------------------+
 * |  telnet(the remaining)   ||   dubbo(complete)    |
 * +--------------------------++----------------------+
 *                            ||
 *                        Magic Code
 *
 * @throws InterruptedException
 */
@Test
public void testTelnetDubboDecoded() throws InterruptedException, IOException {
    ByteBuf dubboByteBuf = createDubboByteBuf();
    ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("test\r".getBytes());
    EmbeddedChannel ch = null;
    try {
        Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo");
        URL url = new URL("dubbo", "localhost", 22226);
        NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler());
        MockHandler mockHandler = new MockHandler((msg) -> {
            if (checkTelnetDecoded(msg)) {
                telnetDubbo.incrementAndGet();
            }
        }, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {

            @Override
            public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
                if (checkDubboDecoded(msg)) {
                    telnetDubbo.incrementAndGet();
                }
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(telnetByteBuf);
        ch.writeInbound(Unpooled.wrappedBuffer(Unpooled.wrappedBuffer("\n".getBytes()), dubboByteBuf));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(2, telnetDubbo.get());
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) NettyCodecAdapter(org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter) Codec2(org.apache.dubbo.remoting.Codec2) MultiMessageHandler(org.apache.dubbo.remoting.transport.MultiMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) ByteBuf(io.netty.buffer.ByteBuf) URL(org.apache.dubbo.common.URL) IOException(java.io.IOException) HeaderExchangeHandler(org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) DecodeHandler(org.apache.dubbo.remoting.transport.DecodeHandler) Test(org.junit.jupiter.api.Test)

Aggregations

ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)23 Test (org.junit.jupiter.api.Test)12 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)9 ExchangeHandlerAdapter (org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter)8 URL (org.apache.dubbo.common.URL)7 ByteBuf (io.netty.buffer.ByteBuf)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 IOException (java.io.IOException)6 Codec2 (org.apache.dubbo.remoting.Codec2)6 Request (org.apache.dubbo.remoting.exchange.Request)6 DecodeHandler (org.apache.dubbo.remoting.transport.DecodeHandler)6 MultiMessageHandler (org.apache.dubbo.remoting.transport.MultiMessageHandler)6 NettyCodecAdapter (org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter)6 Channel (org.apache.dubbo.remoting.Channel)5 RemotingException (org.apache.dubbo.remoting.RemotingException)4 Response (org.apache.dubbo.remoting.exchange.Response)4 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2