use of io.pravega.client.segment.impl.SegmentInputStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testReadWithSegmentTruncatedException.
@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testReadWithSegmentTruncatedException() throws Exception {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
// Prep the mocks.
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
// Mock for the two SegmentInputStreams.
Segment segment = Segment.fromScopedName("Foo/Bar/0");
EventSegmentReader segmentInputStream1 = Mockito.mock(EventSegmentReader.class);
Mockito.when(segmentInputStream1.read(anyLong())).thenThrow(new SegmentTruncatedException());
Mockito.when(segmentInputStream1.getSegmentId()).thenReturn(segment);
EventSegmentReader segmentInputStream2 = Mockito.mock(EventSegmentReader.class);
@Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer = writeInt(stream, 1);
Mockito.when(segmentInputStream2.read(anyLong())).thenReturn(buffer);
Mockito.when(segmentInputStream2.getSegmentId()).thenReturn(Segment.fromScopedName("Foo/test/0"));
Mockito.when(segmentInputStream2.getOffset()).thenReturn(10L);
SegmentInputStreamFactory inputStreamFactory = Mockito.mock(SegmentInputStreamFactory.class);
Mockito.when(inputStreamFactory.createEventReaderForSegment(any(Segment.class), anyInt(), any(Semaphore.class), anyLong())).thenReturn(segmentInputStream1);
// Mock Orderer
Orderer orderer = Mockito.mock(Orderer.class);
Mockito.when(orderer.nextSegment(any(List.class))).thenReturn(segmentInputStream1).thenReturn(segmentInputStream2);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(inputStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
assertThrows(TruncatedDataException.class, () -> reader.readNextEvent(100L));
}
use of io.pravega.client.segment.impl.SegmentInputStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testReadWithEndOfSegmentException.
@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testReadWithEndOfSegmentException() throws Exception {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
// Prep the mocks.
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
// Mock for the two SegmentInputStreams.
Segment segment = Segment.fromScopedName("Foo/Bar/0");
EventSegmentReader segmentInputStream1 = Mockito.mock(EventSegmentReader.class);
Mockito.when(segmentInputStream1.read(anyLong())).thenThrow(new EndOfSegmentException(EndOfSegmentException.ErrorType.END_OFFSET_REACHED));
Mockito.when(segmentInputStream1.getSegmentId()).thenReturn(segment);
EventSegmentReader segmentInputStream2 = Mockito.mock(EventSegmentReader.class);
@Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer = writeInt(stream, 1);
Mockito.when(segmentInputStream2.read(anyLong())).thenReturn(buffer);
Mockito.when(segmentInputStream2.getSegmentId()).thenReturn(Segment.fromScopedName("Foo/test/0"));
Mockito.when(segmentInputStream2.getOffset()).thenReturn(10L);
SegmentInputStreamFactory inputStreamFactory = Mockito.mock(SegmentInputStreamFactory.class);
Mockito.when(inputStreamFactory.createEventReaderForSegment(any(Segment.class), anyInt(), any(Semaphore.class), anyLong())).thenReturn(segmentInputStream1);
// Mock Orderer
Orderer orderer = Mockito.mock(Orderer.class);
Mockito.when(orderer.nextSegment(any(List.class))).thenReturn(segmentInputStream1).thenReturn(segmentInputStream2);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(inputStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
InOrder inOrder = Mockito.inOrder(segmentInputStream1, groupState);
EventRead<byte[]> event = reader.readNextEvent(100L);
assertNotNull(event.getEvent());
// Validate that segmentInputStream1.close() is invoked on reaching endOffset.
Mockito.verify(segmentInputStream1, Mockito.times(1)).close();
SegmentWithRange segmentWithRange = new SegmentWithRange(segment, 0, 1);
// Verify groupstate is not updated before the checkpoint, but is after.
inOrder.verify(groupState, Mockito.times(0)).handleEndOfSegment(segmentWithRange);
Mockito.when(groupState.getCheckpoint()).thenReturn("checkpoint").thenReturn(null);
assertEquals("checkpoint", reader.readNextEvent(0).getCheckpointName());
inOrder.verify(groupState).getCheckpoint();
// Verify groupstate is not updated before the checkpoint, but is after.
inOrder.verify(groupState, Mockito.times(0)).handleEndOfSegment(segmentWithRange);
event = reader.readNextEvent(0);
assertFalse(event.isCheckpoint());
// Now it is called.
inOrder.verify(groupState, Mockito.times(1)).handleEndOfSegment(segmentWithRange);
}
Aggregations