Search in sources :

Example 11 with ByteBufferOutputStream

use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.

the class RevisionDataOutputStreamTests method testRandomOutputExpandable.

/**
 * Tests the RandomRevisionDataOutput class with an {@link ByteBufferOutputStream}.
 */
@Test
public void testRandomOutputExpandable() throws Exception {
    @Cleanup val s = new ByteBufferOutputStream();
    @Cleanup val impl = RevisionDataOutputStream.wrap(s);
    testImpl(impl, s::getData);
}
Also used : lombok.val(lombok.val) Charsets(com.google.common.base.Charsets) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Collectors(java.util.stream.Collectors) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 12 with ByteBufferOutputStream

use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.

the class BitConverterTests method testLong.

/**
 * Tests the {@link BitConverter#writeLong} and {@link BitConverter#readLong}.
 */
@Test
public void testLong() throws IOException {
    test(BitConverter::writeLong, BitConverter::readLong, DataInputStream::readLong, Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L);
    WriteArray<Long> streamWriter = (target, offset, value) -> {
        @Cleanup val s = new ByteBufferOutputStream();
        try {
            BitConverter.writeLong(s, value);
        } catch (IOException ex) {
            throw new CompletionException(ex);
        }
        s.getData().copyTo(target, offset, s.size());
        return s.size();
    };
    test(streamWriter, BitConverter::readLong, DataInputStream::readLong, Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L);
    ReadStream<Long> streamReader = s -> {
        byte[] data = new byte[Long.BYTES];
        Assert.assertEquals(Long.BYTES, s.read(data));
        return BitConverter.readLong(data, 0);
    };
    testStream(BitConverter::writeLong, streamReader, DataInputStream::readLong, Long.MIN_VALUE, Long.MAX_VALUE, -1L, 0L, 1L);
}
Also used : OutputStream(java.io.OutputStream) DataInputStream(java.io.DataInputStream) SneakyThrows(lombok.SneakyThrows) lombok.val(lombok.val) Cleanup(lombok.Cleanup) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) UUID(java.util.UUID) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assert(org.junit.Assert) InputStream(java.io.InputStream) lombok.val(lombok.val) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) CompletionException(java.util.concurrent.CompletionException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 13 with ByteBufferOutputStream

use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.

the class BitConverterTests method testStream.

@SafeVarargs
private final <T> void testStream(WriteStream<T> write, ReadStream<T> read, ReadDataStream<T> dataStreamReader, T... testValues) throws IOException {
    for (T value : testValues) {
        @Cleanup val s = new ByteBufferOutputStream(MAX_LENGTH);
        write.apply(s, value);
        T readValue = read.apply(s.getData().getReader());
        Assert.assertEquals("Unexpected deserialized value.", value, readValue);
        // Use a DataInputStream to verify that the value was correctly encoded.
        @Cleanup val ds = new DataInputStream(s.getData().getReader());
        T dataStreamReadValue = dataStreamReader.apply(ds);
        Assert.assertEquals("Unexpected deserialized value (DataInputStream).", dataStreamReadValue, value);
    }
}
Also used : lombok.val(lombok.val) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) DataInputStream(java.io.DataInputStream) Cleanup(lombok.Cleanup)

Example 14 with ByteBufferOutputStream

use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.

the class RevisionDataStreamCommonTests method testGetRemaining.

/**
 * Tests {@link RevisionDataInput#getRemaining()}.
 */
@Test
public void testGetRemaining() throws Exception {
    @Cleanup val os = new ByteBufferOutputStream();
    @Cleanup val rdos = RevisionDataOutputStream.wrap(os);
    rdos.writeInt(1);
    rdos.writeLong(2L);
    rdos.writeBuffer(new ByteArraySegment(new byte[3]));
    rdos.flush();
    rdos.close();
    // BoundedInputStream header.
    int expectedRemaining = os.getData().getLength() - Integer.BYTES;
    // Use a SequenceInputStream - this will always have available() set to 0.
    @Cleanup val rdis = RevisionDataInputStream.wrap(os.getData().getReader());
    Assert.assertEquals(expectedRemaining, rdis.getRemaining());
    Assert.assertEquals(1, rdis.readInt());
    expectedRemaining -= Integer.BYTES;
    Assert.assertEquals(expectedRemaining, rdis.getRemaining());
    Assert.assertEquals(2L, rdis.readLong());
    expectedRemaining -= Long.BYTES;
    Assert.assertEquals(expectedRemaining, rdis.getRemaining());
    Assert.assertEquals(3, rdis.readArray().length);
    expectedRemaining = 0;
    Assert.assertEquals(expectedRemaining, rdis.getRemaining());
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 15 with ByteBufferOutputStream

use of io.pravega.common.io.ByteBufferOutputStream in project pravega by pravega.

the class RevisionDataStreamCommonTests method testEncodeDecode.

private <T> void testEncodeDecode(BiConsumerWithException<RevisionDataOutputStream, T> write, FunctionWithException<RevisionDataInputStream, T> read, BiFunction<RevisionDataOutputStream, T, Integer> getLength, T value, BiPredicate<T, T> equalityTester) throws Exception {
    @Cleanup val os = new ByteBufferOutputStream();
    @Cleanup val rdos = RevisionDataOutputStream.wrap(os);
    write.accept(rdos, value);
    rdos.close();
    os.close();
    // Subtract 4 because this is the Length being encoded.
    val actualLength = os.size() - Integer.BYTES;
    Assert.assertEquals("Unexpected length for value " + value, (int) getLength.apply(rdos, value), actualLength);
    @Cleanup val rdis = RevisionDataInputStream.wrap(os.getData().getReader());
    val actualValue = read.apply(rdis);
    Assert.assertTrue(String.format("Encoding/decoding failed for %s (decoded %s).", value, actualValue), equalityTester.test(value, actualValue));
}
Also used : lombok.val(lombok.val) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) Cleanup(lombok.Cleanup)

Aggregations

ByteBufferOutputStream (io.pravega.common.io.ByteBufferOutputStream)19 lombok.val (lombok.val)15 Cleanup (lombok.Cleanup)13 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataOutputStream (java.io.DataOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SneakyThrows (lombok.SneakyThrows)3 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)2 AssertExtensions (io.pravega.test.common.AssertExtensions)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 Random (java.util.Random)2 Assert (org.junit.Assert)2 Charsets (com.google.common.base.Charsets)1 ByteBuf (io.netty.buffer.ByteBuf)1 Exceptions (io.pravega.common.Exceptions)1 Futures (io.pravega.common.concurrent.Futures)1