Search in sources :

Example 1 with ActiveMQFrameDecoder2

use of org.apache.activemq.artemis.core.remoting.impl.netty.ActiveMQFrameDecoder2 in project activemq-artemis by apache.

the class ActiveMQFrameDecoder2Test method testOrdinaryFragmentation.

@Test
public void testOrdinaryFragmentation() throws Exception {
    final EmbeddedChannel decoder = new EmbeddedChannel(new ActiveMQFrameDecoder2());
    final byte[] data = new byte[ActiveMQFrameDecoder2Test.MSG_LEN];
    ActiveMQFrameDecoder2Test.rand.nextBytes(data);
    ByteBuf src = Unpooled.buffer(ActiveMQFrameDecoder2Test.MSG_CNT * (ActiveMQFrameDecoder2Test.MSG_LEN + 4));
    while (src.writerIndex() < src.capacity()) {
        src.writeInt(ActiveMQFrameDecoder2Test.MSG_LEN);
        src.writeBytes(data);
    }
    List<ByteBuf> packets = new ArrayList<>();
    while (src.isReadable()) {
        int length = Math.min(ActiveMQFrameDecoder2Test.rand.nextInt(ActiveMQFrameDecoder2Test.FRAGMENT_MAX_LEN), src.readableBytes());
        packets.add(src.readBytes(length));
    }
    int cnt = 0;
    for (ByteBuf p : packets) {
        decoder.writeInbound(p);
        for (; ; ) {
            ByteBuf frame = (ByteBuf) decoder.readInbound();
            if (frame == null) {
                break;
            }
            Assert.assertEquals(4, frame.readerIndex());
            Assert.assertEquals(ActiveMQFrameDecoder2Test.MSG_LEN, frame.readableBytes());
            Assert.assertEquals(Unpooled.wrappedBuffer(data), frame);
            cnt++;
            frame.release();
        }
    }
    Assert.assertEquals(ActiveMQFrameDecoder2Test.MSG_CNT, cnt);
}
Also used : ActiveMQFrameDecoder2(org.apache.activemq.artemis.core.remoting.impl.netty.ActiveMQFrameDecoder2) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 2 with ActiveMQFrameDecoder2

use of org.apache.activemq.artemis.core.remoting.impl.netty.ActiveMQFrameDecoder2 in project activemq-artemis by apache.

the class ActiveMQFrameDecoder2Test method testExtremeFragmentation.

@Test
public void testExtremeFragmentation() throws Exception {
    final EmbeddedChannel decoder = new EmbeddedChannel(new ActiveMQFrameDecoder2());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 4 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 5 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 6 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 7 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 8 }));
    ByteBuf frame = (ByteBuf) decoder.readInbound();
    Assert.assertEquals(4, frame.readerIndex());
    Assert.assertEquals(4, frame.readableBytes());
    Assert.assertEquals(5, frame.getByte(4));
    Assert.assertEquals(6, frame.getByte(5));
    Assert.assertEquals(7, frame.getByte(6));
    Assert.assertEquals(8, frame.getByte(7));
    frame.release();
}
Also used : ActiveMQFrameDecoder2(org.apache.activemq.artemis.core.remoting.impl.netty.ActiveMQFrameDecoder2) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 ActiveMQFrameDecoder2 (org.apache.activemq.artemis.core.remoting.impl.netty.ActiveMQFrameDecoder2)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1