use of io.pravega.client.segment.impl.SegmentMetadataClient in project pravega by pravega.
the class SegmentIteratorTest method testTruncate.
@Test(timeout = 5000)
public void testTruncate() {
MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
Segment segment = new Segment("Scope", "Stream", 1);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
}, config, "");
sendData("1", outputStream);
sendData("2", outputStream);
sendData("3", outputStream);
SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, "");
long length = metadataClient.getSegmentInfo().getWriteOffset();
@Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
assertEquals("1", iter.next());
long segmentLength = metadataClient.fetchCurrentSegmentLength();
assertEquals(0, segmentLength % 3);
metadataClient.truncateSegment(segment, segmentLength * 2 / 3);
AssertExtensions.assertThrows(TruncatedDataException.class, () -> iter.next());
@Cleanup SegmentIteratorImpl<String> iter2 = new SegmentIteratorImpl<>(factory, segment, stringSerializer, segmentLength * 2 / 3, length);
assertTrue(iter2.hasNext());
assertEquals("3", iter2.next());
assertFalse(iter.hasNext());
}
use of io.pravega.client.segment.impl.SegmentMetadataClient in project pravega by pravega.
the class SegmentIteratorTest method testHasNext.
@Test(timeout = 5000)
public void testHasNext() {
MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
Segment segment = new Segment("Scope", "Stream", 1);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
}, config, "");
sendData("1", outputStream);
sendData("2", outputStream);
sendData("3", outputStream);
SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, "");
long length = metadataClient.getSegmentInfo().getWriteOffset();
@Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
assertTrue(iter.hasNext());
assertTrue(iter.hasNext());
assertEquals("1", iter.next());
assertEquals("2", iter.next());
assertTrue(iter.hasNext());
assertEquals("3", iter.next());
assertFalse(iter.hasNext());
assertThrows(NoSuchElementException.class, () -> iter.next());
assertFalse(iter.hasNext());
}
use of io.pravega.client.segment.impl.SegmentMetadataClient in project pravega by pravega.
the class EventStreamReaderTest method testDataTruncated.
@Test(timeout = 10000)
public void testDataTruncated() throws SegmentSealedException, ReinitializationRequiredException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
Segment segment = Segment.fromScopedName("Foo/Bar/0");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
SegmentMetadataClient metadataClient = segmentStreamFactory.createSegmentMetadataClient(segment, "");
ByteBuffer buffer1 = writeInt(stream, 1);
ByteBuffer buffer2 = writeInt(stream, 2);
ByteBuffer buffer3 = writeInt(stream, 3);
long length = metadataClient.fetchCurrentSegmentLength();
assertEquals(0, length % 3);
EventRead<byte[]> event1 = reader.readNextEvent(0);
assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
metadataClient.truncateSegment(segment, length / 3);
assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
metadataClient.truncateSegment(segment, length);
ByteBuffer buffer4 = writeInt(stream, 4);
AssertExtensions.assertThrows(TruncatedDataException.class, () -> reader.readNextEvent(0));
assertEquals(buffer4, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
AssertExtensions.assertThrows(NoSuchEventException.class, () -> reader.fetchEvent(event1.getEventPointer()));
reader.close();
}
Aggregations