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));
}
}
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;
}
}
}
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);
}
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);
}
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());
}
Aggregations