Search in sources :

Example 1 with NettyCodecAdapter

use of org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter in project dubbo by alibaba.

the class DubboTelnetDecodeTest method testDubboDubboDecoded.

/**
 * dubbo and dubbo request
 *
 * <p>
 * First ByteBuf (firstDubboByteBuf):
 * ++-------------------------------------------------+
 * ||               dubbo(incomplete)                 |
 * ++-------------------------------------------------+
 * ||
 * Magic Code
 * <p>
 *
 * <p>
 * Second ByteBuf (secondDubboByteBuf):
 * +-------------------------++-----------------------+
 * |  dubbo(the remaining)   ||    dubbo(complete)    |
 * +-------------------------++-----------------------+
 *                           ||
 *                       Magic Code
 *
 * @throws InterruptedException
 */
@Test
public void testDubboDubboDecoded() throws InterruptedException, IOException {
    ByteBuf dubboByteBuf = createDubboByteBuf();
    ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50);
    ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes() - 50);
    ByteBuf secondDubboByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, dubboByteBuf);
    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(null, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {

            @Override
            public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
                if (checkDubboDecoded(msg)) {
                    dubboDubbo.incrementAndGet();
                }
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(firstDubboByteBuf);
        ch.writeInbound(secondDubboByteBuf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(2, dubboDubbo.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)

Example 2 with NettyCodecAdapter

use of org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter in project dubbo by alibaba.

the class DubboTelnetDecodeTest method testTelnetDecode.

/**
 * just telnet request
 *
 * @throws InterruptedException
 */
@Test
public void testTelnetDecode() throws InterruptedException {
    ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("test\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)) {
                telnet.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(telnetByteBuf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(1, telnet.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)

Example 3 with NettyCodecAdapter

use of org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter 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 4 with NettyCodecAdapter

use of org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter 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)

Example 5 with NettyCodecAdapter

use of org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter in project dubbo by alibaba.

the class DubboTelnetDecodeTest method testDubboDecode.

/**
 * just dubbo request
 *
 * @throws InterruptedException
 */
@Test
public void testDubboDecode() throws InterruptedException, IOException {
    ByteBuf dubboByteBuf = createDubboByteBuf();
    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(null, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {

            @Override
            public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
                if (checkDubboDecoded(msg)) {
                    dubbo.incrementAndGet();
                }
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(dubboByteBuf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ch != null) {
            ch.close().await(200, TimeUnit.MILLISECONDS);
        }
    }
    TimeUnit.MILLISECONDS.sleep(100);
    Assertions.assertEquals(1, dubbo.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

ByteBuf (io.netty.buffer.ByteBuf)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 IOException (java.io.IOException)6 URL (org.apache.dubbo.common.URL)6 Codec2 (org.apache.dubbo.remoting.Codec2)6 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)6 ExchangeHandlerAdapter (org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter)6 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)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 Test (org.junit.jupiter.api.Test)6 Disabled (org.junit.jupiter.api.Disabled)1