Search in sources :

Example 11 with HeaderExchangeHandler

use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler 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)

Example 12 with HeaderExchangeHandler

use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.

the class DubboTelnetDecodeTest method testDubboTelnetDecoded.

/**
 * dubbo and telnet request
 *
 * <p>
 * First ByteBuf:
 * ++-------------------------------------------------+
 * ||               dubbo(incomplete)                 |
 * ++-------------------------------------------------+
 * ||
 * Magic Code
 *
 * <p>
 * Second ByteBuf:
 * +--------------------------------------------------+
 * |  dubbo(the remaining)  |     telnet(complete)    |
 * +--------------------------------------------------+
 *
 * @throws InterruptedException
 */
@Test
public void testDubboTelnetDecoded() throws InterruptedException, IOException {
    ByteBuf dubboByteBuf = createDubboByteBuf();
    ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50);
    ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes());
    ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("\r\n".getBytes());
    ByteBuf secondByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, telnetByteBuf);
    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)) {
                dubboTelnet.incrementAndGet();
            }
        }, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {

            @Override
            public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
                if (checkDubboDecoded(msg)) {
                    dubboTelnet.incrementAndGet();
                }
                return getDefaultFuture();
            }
        }))));
        ch = new LocalEmbeddedChannel();
        ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
        ch.writeInbound(firstDubboByteBuf);
        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, dubboTelnet.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)12 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)12 Test (org.junit.jupiter.api.Test)12 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 Channel (org.apache.dubbo.remoting.Channel)6 Codec2 (org.apache.dubbo.remoting.Codec2)6 Request (org.apache.dubbo.remoting.exchange.Request)6 ExchangeHandlerAdapter (org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter)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 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)3 Response (org.apache.dubbo.remoting.exchange.Response)3 CompletableFuture (java.util.concurrent.CompletableFuture)1 RemotingException (org.apache.dubbo.remoting.RemotingException)1 Disabled (org.junit.jupiter.api.Disabled)1