Search in sources :

Example 91 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class RollingStorageTestBase method testWriteOnRollOverBoundary.

@Test
public void testWriteOnRollOverBoundary() throws Exception {
    final String segmentName = "Segment";
    // Really small rolling length.
    final int maxLength = 3;
    val seq1 = "01234";
    val seq2 = "56789";
    val totalWriteLength = seq1.length() + seq2.length();
    @Cleanup val s = createStorage();
    s.initialize(1);
    val writeHandle = s.create(segmentName, new SegmentRollingPolicy(maxLength), TIMEOUT).thenCompose(v -> s.openWrite(segmentName)).join();
    val byteInputStream1 = new ByteArrayInputStream(seq1.getBytes());
    val byteInputStream2 = new ByteArrayInputStream(seq2.getBytes());
    val sequenceInputStream = new SequenceInputStream(byteInputStream1, byteInputStream2);
    // This write should cause 3 rollovers.
    s.write(writeHandle, 0, sequenceInputStream, totalWriteLength, TIMEOUT).join();
    // Check rollover actually happened as expected.
    if (useOldLayout) {
        RollingSegmentHandle checkHandle = (RollingSegmentHandle) s.openWrite(segmentName).join();
        val chunks = checkHandle.chunks();
        int numberOfRollovers = totalWriteLength / maxLength;
        Assert.assertEquals(numberOfRollovers + 1, chunks.size());
        for (int i = 0; i < numberOfRollovers; i++) {
            Assert.assertEquals(maxLength * i, chunks.get(i).getStartOffset());
            Assert.assertEquals(maxLength, chunks.get(i).getLength());
        }
        // Last chunk has index == numberOfRollovers, as list is 0 based.
        Assert.assertEquals(numberOfRollovers * maxLength, chunks.get(numberOfRollovers).getStartOffset());
        Assert.assertEquals(1, chunks.get(numberOfRollovers).getLength());
        // Now validate the contents written.
        val readHandle = s.openRead(segmentName).join();
        byte[] output = new byte[totalWriteLength];
        s.read(readHandle, 0, output, 0, totalWriteLength, TIMEOUT).join();
        Assert.assertEquals(seq1 + seq2, new String(output));
    }
}
Also used : lombok.val(lombok.val) Storage(io.pravega.segmentstore.storage.Storage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) SequenceInputStream(java.io.SequenceInputStream) lombok.val(lombok.val) AsyncStorageWrapper(io.pravega.segmentstore.storage.AsyncStorageWrapper) Cleanup(lombok.Cleanup) Random(java.util.Random) Test(org.junit.Test) ByteArrayInputStream(java.io.ByteArrayInputStream) StorageTestBase(io.pravega.segmentstore.storage.StorageTestBase) SyncStorage(io.pravega.segmentstore.storage.SyncStorage) Assert(org.junit.Assert) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 92 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class NoOpStorageUserDataWriteOnlyTests method testWrite.

@Override
@Test
public void testWrite() throws Exception {
    String segmentName = "foo_write";
    int appendCount = 10;
    try (Storage s = createStorage()) {
        s.initialize(DEFAULT_EPOCH);
        createSegment(segmentName, s);
        val writeHandle = s.openWrite(segmentName).join();
        long offset = 0;
        for (int j = 0; j < appendCount; j++) {
            byte[] writeData = String.format(APPEND_FORMAT, segmentName, j).getBytes();
            val dataStream = new SequenceInputStream(new ByteArrayInputStream(writeData), new ByteArrayInputStream(new byte[100]));
            s.write(writeHandle, offset, dataStream, writeData.length, TIMEOUT).join();
            offset += writeData.length;
        }
    }
}
Also used : lombok.val(lombok.val) Storage(io.pravega.segmentstore.storage.Storage) SyncStorage(io.pravega.segmentstore.storage.SyncStorage) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 93 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class CompositeBufferViewTests method testGetCopy.

/**
 * Tests {@link CompositeBufferView#getCopy()}.
 */
@Test
public void testGetCopy() throws IOException {
    val components = createComponents();
    val cb = BufferView.wrap(components);
    val expectedSize = components.stream().mapToInt(BufferView::getLength).sum();
    val expected = StreamHelpers.readAll(new SequenceInputStream(Iterators.asEnumeration(components.stream().map(BufferView::getReader).iterator())), expectedSize);
    val actual = cb.getCopy();
    Assert.assertArrayEquals("", expected, actual);
}
Also used : lombok.val(lombok.val) SequenceInputStream(java.io.SequenceInputStream) Test(org.junit.Test)

Example 94 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class CompositeBufferViewTests method testWrapRecursive.

/**
 * Tests {@link BufferView#wrap(List)}.
 */
@Test
public void testWrapRecursive() throws IOException {
    val b1 = new ByteArraySegment(new byte[] { 1 });
    val b2 = new ByteArraySegment(new byte[] { 2 });
    val b3 = new ByteArraySegment(new byte[] { 3 });
    val c1 = BufferView.wrap(Arrays.asList(b1, b2));
    val c2 = BufferView.wrap(Arrays.asList(c1, b3));
    Assert.assertEquals(b1.getLength() + b2.getLength() + b3.getLength(), c2.getLength());
    AssertExtensions.assertStreamEquals("", new SequenceInputStream(Iterators.asEnumeration(Arrays.asList(b1.getReader(), b2.getReader(), b3.getReader()).iterator())), c2.getReader(), c2.getLength());
    val contentBufs = getContents(c2);
    val expectedContentBufs = Stream.of(b1, b2, b3).flatMap(b -> getContents(b).stream()).collect(Collectors.toList());
    AssertExtensions.assertListEquals("", expectedContentBufs, contentBufs, ByteBuffer::equals);
}
Also used : lombok.val(lombok.val) OutputStream(java.io.OutputStream) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertExtensions(io.pravega.test.common.AssertExtensions) SequenceInputStream(java.io.SequenceInputStream) lombok.val(lombok.val) IOException(java.io.IOException) Random(java.util.Random) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Iterators(com.google.common.collect.Iterators) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) Assert(org.junit.Assert) Collections(java.util.Collections) StreamHelpers(io.pravega.common.io.StreamHelpers) SequenceInputStream(java.io.SequenceInputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 95 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class CompositeBufferViewTests method testCopyToOutputStream.

/**
 * Tests {@link CompositeBufferView#copyTo(OutputStream)}.
 */
@Test
public void testCopyToOutputStream() throws IOException {
    val components = createComponents();
    val cb = BufferView.wrap(components);
    val expectedSize = components.stream().mapToInt(BufferView::getLength).sum();
    val expected = StreamHelpers.readAll(new SequenceInputStream(Iterators.asEnumeration(components.stream().map(BufferView::getReader).iterator())), expectedSize);
    val actual = new ByteArrayOutputStream();
    cb.copyTo(actual);
    Assert.assertEquals(expectedSize, actual.size());
    Assert.assertArrayEquals("", expected, actual.toByteArray());
}
Also used : lombok.val(lombok.val) SequenceInputStream(java.io.SequenceInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SequenceInputStream (java.io.SequenceInputStream)123 InputStream (java.io.InputStream)78 ByteArrayInputStream (java.io.ByteArrayInputStream)67 IOException (java.io.IOException)47 ArrayList (java.util.ArrayList)32 FileInputStream (java.io.FileInputStream)24 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Vector (java.util.Vector)10 Test (org.junit.Test)10 FileOutputStream (java.io.FileOutputStream)9 List (java.util.List)9 lombok.val (lombok.val)9 File (java.io.File)8 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 InputStreamReader (java.io.InputStreamReader)7 ByteBuffer (java.nio.ByteBuffer)6 Support_ASimpleInputStream (tests.support.Support_ASimpleInputStream)6 Reader (java.io.Reader)5