Search in sources :

Example 6 with SegmentInputStreamFactory

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));
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) Semaphore(java.util.concurrent.Semaphore) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) Test(org.junit.Test)

Example 7 with SegmentInputStreamFactory

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);
}
Also used : InOrder(org.mockito.InOrder) EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Semaphore(java.util.concurrent.Semaphore) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) Test(org.junit.Test)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)7 SegmentInputStreamFactory (io.pravega.client.segment.impl.SegmentInputStreamFactory)7 Cleanup (lombok.Cleanup)7 Test (org.junit.Test)7 EventSegmentReader (io.pravega.client.segment.impl.EventSegmentReader)6 MockController (io.pravega.client.stream.mock.MockController)6 Controller (io.pravega.client.control.impl.Controller)4 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)4 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)4 Semaphore (java.util.concurrent.Semaphore)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)3 SegmentMetadataClientFactory (io.pravega.client.segment.impl.SegmentMetadataClientFactory)3 InOrder (org.mockito.InOrder)3 DelegationTokenProvider (io.pravega.client.security.auth.DelegationTokenProvider)2 EndOfSegmentException (io.pravega.client.segment.impl.EndOfSegmentException)2 SegmentInfo (io.pravega.client.segment.impl.SegmentInfo)2 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)2 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)2 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)2