use of io.pravega.client.segment.impl.SegmentOutputStream 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.SegmentOutputStream 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.SegmentOutputStream in project pravega by pravega.
the class EventStreamReaderTest method testReleaseSegment.
@Test(timeout = 10000)
public void testReleaseSegment() 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 segment1 = Segment.fromScopedName("Foo/Bar/0");
Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment1, 0L, segment2, 0L)).thenReturn(Collections.emptyMap());
SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
writeInt(stream1, 1);
writeInt(stream2, 2);
reader.readNextEvent(0);
List<SegmentInputStream> readers = reader.getReaders();
assertEquals(2, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
Assert.assertEquals(segment2, readers.get(1).getSegmentId());
Mockito.when(groupState.getCheckpoint()).thenReturn("checkpoint");
assertTrue(reader.readNextEvent(0).isCheckpoint());
Mockito.when(groupState.getCheckpoint()).thenReturn(null);
Mockito.when(groupState.findSegmentToReleaseIfRequired()).thenReturn(segment2);
Mockito.when(groupState.releaseSegment(Mockito.eq(segment2), Mockito.anyLong(), Mockito.anyLong())).thenReturn(true);
assertFalse(reader.readNextEvent(0).isCheckpoint());
Mockito.verify(groupState).releaseSegment(Mockito.eq(segment2), Mockito.anyLong(), Mockito.anyLong());
readers = reader.getReaders();
assertEquals(1, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
reader.close();
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamReaderTest method testAcquireSegment.
@SuppressWarnings("unused")
@Test(timeout = 10000)
public void testAcquireSegment() 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 segment1 = Segment.fromScopedName("Foo/Bar/0");
Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment1, 0L)).thenReturn(ImmutableMap.of(segment2, 0L)).thenReturn(Collections.emptyMap());
SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
writeInt(stream1, 1);
writeInt(stream1, 2);
writeInt(stream2, 3);
writeInt(stream2, 4);
reader.readNextEvent(0);
List<SegmentInputStream> readers = reader.getReaders();
assertEquals(1, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
reader.readNextEvent(0);
readers = reader.getReaders();
assertEquals(2, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
Assert.assertEquals(segment2, readers.get(1).getSegmentId());
reader.close();
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamReaderTest method testEndOfSegmentWithoutSuccessors.
@Test(timeout = 10000)
public void testEndOfSegmentWithoutSuccessors() 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, "");
ByteBuffer buffer = writeInt(stream, 1);
EventRead<byte[]> read = reader.readNextEvent(0);
byte[] event = read.getEvent();
assertEquals(buffer, ByteBuffer.wrap(event));
read = reader.readNextEvent(0);
assertNull(read.getEvent());
read = reader.readNextEvent(0);
assertNull(read.getEvent());
assertEquals(0, reader.getReaders().size());
reader.close();
}
Aggregations