Skip to content

Commit

Permalink
Ensure direct bytebuf for both compression and decompression test
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat committed May 28, 2020
1 parent cc4d33a commit 7119f10
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;

import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

Expand All @@ -50,28 +51,33 @@ public Object[][] codecProvider() {
@Test(dataProvider = "codecs")
void testCompressDecompress(CompressionCodec c1, CompressionCodec c2) throws IOException {
byte[] data = text.getBytes();
ByteBuf raw = Unpooled.directBuffer();
ByteBuf raw = PulsarByteBufAllocator.DEFAULT.directBuffer();
raw.writeBytes(data);

ByteBuf compressed = c1.encode(raw);
assertEquals(raw.readableBytes(), data.length);

int compressedSize = compressed.readableBytes();

ByteBuf uncompressed = c2.decode(compressed, data.length);
// Copy into a direct byte buf
ByteBuf compressedDirect = PulsarByteBufAllocator.DEFAULT.directBuffer();
compressedDirect.writeBytes(compressed);

assertEquals(compressed.readableBytes(), compressedSize);
ByteBuf uncompressed = c2.decode(compressedDirect, data.length);

assertEquals(compressedDirect.readableBytes(), compressedSize);

assertEquals(uncompressed.readableBytes(), data.length);
assertEquals(uncompressed, raw);

raw.release();
compressed.release();
uncompressed.release();
compressedDirect.release();

// Verify compression codecs have the same behavior with buffers ref counting
assertEquals(raw.refCnt(), 0);
assertEquals(compressed.refCnt(), 0);
assertEquals(compressed.refCnt(), 0);
assertEquals(compressedDirect.refCnt(), 0);
assertEquals(compressedDirect.refCnt(), 0);
}
}

0 comments on commit 7119f10

Please sign in to comment.