Search in sources :

Example 1 with EncodedMessageBatch

use of com.rabbitmq.stream.impl.Client.EncodedMessageBatch in project rabbitmq-stream-java-client by rabbitmq.

the class CompressionCodecsTest method compressedEncodedMessageBatch.

@ParameterizedTest
@MethodSource
void compressedEncodedMessageBatch(CompressionCodec compressionCodec, CompressionCodec decompressionCodec) throws IOException {
    assertThat(compressionCodec.code()).isEqualTo(decompressionCodec.code());
    ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
    int messageCount = 100;
    EncodedMessageBatch encodedMessageBatch = new CompressedEncodedMessageBatch(allocator, compressionCodec, messageCount);
    List<EncodedMessage> encodedMessages = new ArrayList<>(messageCount);
    IntStream.range(0, messageCount).forEach(i -> {
        byte[] body = ("message " + i).getBytes(StandardCharsets.UTF_8);
        EncodedMessage encodedMessage = new EncodedMessage(body.length, body);
        encodedMessageBatch.add(encodedMessage);
        encodedMessages.add(encodedMessage);
    });
    encodedMessageBatch.close();
    int plainSize = encodedMessages.stream().mapToInt(EncodedMessage::getSize).sum() + encodedMessages.size() * 4;
    int compressedSize = encodedMessageBatch.sizeInBytes();
    ByteBuf destinationBb = allocator.buffer(compressedSize);
    encodedMessageBatch.write(destinationBb);
    assertThat(compressedSize).isLessThan(plainSize);
    assertThat(destinationBb.writerIndex()).isEqualTo(compressedSize);
    ByteBuf outBb = allocator.buffer(plainSize);
    destinationBb.readerIndex(0);
    InputStream inputStream = decompressionCodec.decompress(new ByteBufInputStream(destinationBb));
    int n;
    while (-1 != (n = inputStream.read())) {
        outBb.writeByte(n);
    }
    List<EncodedMessage> decompressedMessages = new ArrayList<>(messageCount);
    while (outBb.isReadable()) {
        int size = outBb.readInt();
        byte[] msg = new byte[size];
        outBb.readBytes(msg);
        decompressedMessages.add(new EncodedMessage(size, msg));
    }
    assertThat(decompressedMessages).hasSameSizeAs(encodedMessages);
    IntStream.range(0, messageCount).forEach(i -> {
        EncodedMessage originalMessage = encodedMessages.get(i);
        EncodedMessage decompressedMessage = decompressedMessages.get(i);
        assertThat(decompressedMessage.getSize()).isEqualTo(originalMessage.getSize());
        assertThat(decompressedMessage.getData()).isEqualTo(originalMessage.getData());
    });
    destinationBb.release();
    outBb.release();
}
Also used : CompressedEncodedMessageBatch(com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EncodedMessageBatch(com.rabbitmq.stream.impl.Client.EncodedMessageBatch) CompressedEncodedMessageBatch(com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

EncodedMessage (com.rabbitmq.stream.Codec.EncodedMessage)1 CompressedEncodedMessageBatch (com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch)1 EncodedMessageBatch (com.rabbitmq.stream.impl.Client.EncodedMessageBatch)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1