use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testEventPointer.
@Test
public void testEventPointer() throws SegmentSealedException, NoSuchEventException, ReaderNotInReaderGroupException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
Segment segment = Segment.fromScopedName("Foo/Bar/0");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment, 0, 1), 0L)).thenReturn(Collections.emptyMap());
Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
@Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer1 = writeInt(stream, 1);
ByteBuffer buffer2 = writeInt(stream, 2);
ByteBuffer buffer3 = writeInt(stream, 3);
EventRead<byte[]> event1 = reader.readNextEvent(0);
EventRead<byte[]> event2 = reader.readNextEvent(0);
EventRead<byte[]> event3 = reader.readNextEvent(0);
assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
assertEquals(buffer2, ByteBuffer.wrap(event2.getEvent()));
assertEquals(buffer3, ByteBuffer.wrap(event3.getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
assertEquals(buffer1, ByteBuffer.wrap(reader.fetchEvent(event1.getEventPointer())));
assertEquals(buffer3, ByteBuffer.wrap(reader.fetchEvent(event3.getEventPointer())));
assertEquals(buffer2, ByteBuffer.wrap(reader.fetchEvent(event2.getEventPointer())));
reader.close();
}
use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testSilentCheckpointFollowingCheckpoint.
@Test(timeout = 10000)
public void testSilentCheckpointFollowingCheckpoint() throws SegmentSealedException, ReaderNotInReaderGroupException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
Segment segment = Segment.fromScopedName("Foo/Bar/0");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment, 0, 1), 0L)).thenReturn(Collections.emptyMap());
Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
@Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer = writeInt(stream, 1);
Mockito.doReturn(true).when(groupState).isCheckpointSilent(Mockito.eq(ReaderGroupImpl.SILENT + "Foo"));
Mockito.when(groupState.getCheckpoint()).thenReturn("Bar").thenReturn(ReaderGroupImpl.SILENT + "Foo").thenReturn(null);
EventRead<byte[]> eventRead = reader.readNextEvent(10000);
assertTrue(eventRead.isCheckpoint());
assertNull(eventRead.getEvent());
assertEquals("Bar", eventRead.getCheckpointName());
assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
InOrder order = Mockito.inOrder(groupState);
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq("Bar"), Mockito.any());
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq(ReaderGroupImpl.SILENT + "Foo"), Mockito.any());
order.verify(groupState).getCheckpoint();
reader.close();
}
use of io.pravega.client.stream.mock.MockSegmentStreamFactory 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.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testSilentCheckpointFollowingCheckpointFollowingSilentCheckpoint.
@Test(timeout = 10000)
public void testSilentCheckpointFollowingCheckpointFollowingSilentCheckpoint() throws SegmentSealedException, ReaderNotInReaderGroupException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
Segment segment = Segment.fromScopedName("Foo/Bar/0");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment, 0, 1), 0L)).thenReturn(Collections.emptyMap());
@Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer = writeInt(stream, 1);
Mockito.doReturn(true).when(groupState).isCheckpointSilent(Mockito.startsWith(ReaderGroupImpl.SILENT));
Mockito.when(groupState.getCheckpoint()).thenReturn(ReaderGroupImpl.SILENT + "Foo").thenReturn("Bar").thenReturn(ReaderGroupImpl.SILENT + "Baz").thenReturn(null);
Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
EventRead<byte[]> eventRead = reader.readNextEvent(10000);
assertTrue(eventRead.isCheckpoint());
assertNull(eventRead.getEvent());
assertEquals("Bar", eventRead.getCheckpointName());
InOrder order = Mockito.inOrder(groupState);
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq(ReaderGroupImpl.SILENT + "Foo"), Mockito.any());
assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq("Bar"), Mockito.any());
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq(ReaderGroupImpl.SILENT + "Baz"), Mockito.any());
order.verify(groupState).findSegmentToReleaseIfRequired();
order.verify(groupState).getCheckpoint();
reader.close();
}
use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class WatermarkReaderImplTest method testUpdates.
@Test
public void testUpdates() {
Stream stream = new StreamImpl("Scope", "streamName");
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
@Cleanup MockClientFactory clientFactory = new MockClientFactory("Scope", segmentStreamFactory);
String markStream = NameUtils.getMarkStreamForStream("streamName");
createScopeAndStream("Scope", markStream, clientFactory.getController());
RevisionedStreamClient<Watermark> writer = clientFactory.createRevisionedStreamClient(markStream, new WatermarkSerializer(), SynchronizerConfig.builder().build());
InlineExecutor executor = new InlineExecutor();
@Cleanup WatermarkReaderImpl impl = new WatermarkReaderImpl(stream, writer, executor);
SegmentWithRange s0 = new SegmentWithRange(new Segment(stream.getScope(), stream.getStreamName(), 0), 0, 0.5);
SegmentWithRange s1 = new SegmentWithRange(new Segment(stream.getScope(), stream.getStreamName(), 1), 0.5, 1);
SegmentWithRange s2 = new SegmentWithRange(new Segment(stream.getScope(), stream.getStreamName(), 2), 0, 0.5);
SegmentWithRange s3 = new SegmentWithRange(new Segment(stream.getScope(), stream.getStreamName(), 3), 0.5, 1);
Map<SegmentWithRange, Long> m1 = ImmutableMap.of(s0, 0L, s1, 0L);
Map<SegmentWithRange, Long> m2 = ImmutableMap.of(s0, 2L, s1, 0L);
Map<SegmentWithRange, Long> m3 = ImmutableMap.of(s0, 2L, s1, 2L);
Map<SegmentWithRange, Long> m4 = ImmutableMap.of(s2, 0L, s1, 2L);
Map<SegmentWithRange, Long> m5 = ImmutableMap.of(s2, 4L, s1, 2L);
Map<SegmentWithRange, Long> m6 = ImmutableMap.of(s2, 4L, s1, 4L);
Map<SegmentWithRange, Long> m7 = ImmutableMap.of(s2, 4L, s3, 0L);
Map<SegmentWithRange, Long> m8 = ImmutableMap.of(s2, 6L, s3, 4L);
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m1)).lowerTimeBound(10).upperTimeBound(19).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m2)).lowerTimeBound(20).upperTimeBound(29).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m3)).lowerTimeBound(30).upperTimeBound(39).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m4)).lowerTimeBound(40).upperTimeBound(49).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m5)).lowerTimeBound(50).upperTimeBound(59).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m6)).lowerTimeBound(60).upperTimeBound(69).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m7)).lowerTimeBound(70).upperTimeBound(79).build());
writer.writeUnconditionally(Watermark.builder().streamCut(convert(m8)).lowerTimeBound(80).upperTimeBound(89).build());
assertEquals(null, impl.getTimeWindow().getLowerTimeBound());
assertEquals(null, impl.getTimeWindow().getUpperTimeBound());
impl.advanceTo(ImmutableMap.of(s0, 1L, s1, 0L));
assertEquals(10, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(29, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s0, 3L, s1, 0L));
assertEquals(20, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(49, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s0, 5L, s1, 0L));
assertEquals(20, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(49, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s0, 6L, s1, 0L));
assertEquals(20, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(49, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s0, 6L, s1, 1L));
assertEquals(20, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(49, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s0, 6L, s1, 3L));
assertEquals(30, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(69, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 0L, s1, 3L));
assertEquals(40, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(69, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 4L, s1, 3L));
assertEquals(50, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(69, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 4L, s1, 5L));
assertEquals(60, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(79, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 4L, s3, 1L));
assertEquals(70, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(89, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 5L, s3, 1L));
assertEquals(70, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(89, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 5L, s3, 5L));
assertEquals(70, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(89, impl.getTimeWindow().getUpperTimeBound().longValue());
impl.advanceTo(ImmutableMap.of(s2, 6L, s3, 5L));
assertEquals(80, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(null, impl.getTimeWindow().getUpperTimeBound());
impl.advanceTo(ImmutableMap.of(s2, 7L, s3, 7L));
assertEquals(80, impl.getTimeWindow().getLowerTimeBound().longValue());
assertEquals(null, impl.getTimeWindow().getUpperTimeBound());
}
Aggregations