use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class SynchronizerTest method testConsistency.
@Test(timeout = 20000)
public void testConsistency() {
String streamName = "streamName";
String scope = "scope";
MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
@Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
createScopeAndStream(streamName, scope, clientFactory.getController());
@Cleanup StateSynchronizer<RevisionedImpl> syncA = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
@Cleanup StateSynchronizer<RevisionedImpl> syncB = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
syncA.initialize(new RegularUpdate("Foo"));
AtomicInteger callCount = new AtomicInteger(0);
syncB.updateState((state, updates) -> {
callCount.incrementAndGet();
updates.add(new RegularUpdate("Bar"));
});
assertEquals(1, callCount.get());
assertEquals("Foo", syncA.getState().value);
syncA.updateState((state, updates) -> {
callCount.incrementAndGet();
updates.add(new RegularUpdate("Baz"));
});
assertEquals(3, callCount.get());
assertEquals("Baz", syncA.getState().value);
syncB.fetchUpdates();
assertEquals("Baz", syncA.getState().value);
syncB.updateState((state, updates) -> {
callCount.incrementAndGet();
updates.add(new RegularUpdate("Bat"));
});
assertEquals(4, callCount.get());
assertEquals("Baz", syncA.getState().value);
syncA.fetchUpdates();
assertEquals("Bat", syncA.getState().value);
}
use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class SynchronizerTest method testCompactionShrinksSet.
@Test(timeout = 20000)
public void testCompactionShrinksSet() {
String streamName = "testCompactionShrinksSet";
String scope = "scope";
MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
@Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
createScopeAndStream(streamName, scope, clientFactory.getController());
SetSynchronizer<String> set = SetSynchronizer.createNewSet(streamName, clientFactory);
RevisionedStreamClient<byte[]> rsc = clientFactory.createRevisionedStreamClient(streamName, new ByteArraySerializer(), SynchronizerConfig.builder().build());
set.add("Foo");
assertNull(rsc.getMark());
set.add("Bar");
assertNull(rsc.getMark());
set.clear();
assertNotNull(rsc.getMark());
Iterator<?> iter = rsc.readFrom(rsc.getMark());
assertTrue(iter.hasNext());
iter.next();
assertFalse(iter.hasNext());
set.add("Foo2");
assertNotNull(rsc.getMark());
assertEquals(1, set.getCurrentSize());
}
use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testCheckpoint.
@Test(timeout = 10000)
public void testCheckpoint() 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.when(groupState.getCheckpoint()).thenReturn("Foo").thenReturn(null);
EventRead<byte[]> eventRead = reader.readNextEvent(0);
assertTrue(eventRead.isCheckpoint());
assertNull(eventRead.getEvent());
assertEquals("Foo", eventRead.getCheckpointName());
InOrder order = Mockito.inOrder(groupState);
order.verify(groupState).getCheckpoint();
order.verify(groupState, Mockito.never()).checkpoint(Mockito.anyString(), Mockito.any());
assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
order.verify(groupState).checkpoint(Mockito.eq("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 testDataTruncated.
@Test(timeout = 10000)
public void testDataTruncated() 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());
@Cleanup SegmentMetadataClient metadataClient = segmentStreamFactory.createSegmentMetadataClient(segment, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer1 = writeInt(stream, 1);
ByteBuffer buffer2 = writeInt(stream, 2);
writeInt(stream, 3);
long length = metadataClient.fetchCurrentSegmentLength().join();
assertEquals(0, length % 3);
EventRead<byte[]> event1 = reader.readNextEvent(0);
assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
metadataClient.truncateSegment(length / 3).join();
assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
metadataClient.truncateSegment(length).join();
ByteBuffer buffer4 = writeInt(stream, 4);
assertThrows(TruncatedDataException.class, () -> reader.readNextEvent(0));
assertEquals(buffer4, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
assertThrows(NoSuchEventException.class, () -> reader.fetchEvent(event1.getEventPointer()));
reader.close();
}
use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class EventStreamReaderTest method testCheckpointFollowingSilentCheckpointFollowingCheckpoint.
@Test(timeout = 10000)
public void testCheckpointFollowingSilentCheckpointFollowingCheckpoint() 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("Foo").thenReturn(ReaderGroupImpl.SILENT + "Bar").thenReturn("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("Foo", eventRead.getCheckpointName());
eventRead = reader.readNextEvent(0);
assertTrue(eventRead.isCheckpoint());
assertNull(eventRead.getEvent());
assertEquals("Baz", eventRead.getCheckpointName());
InOrder order = Mockito.inOrder(groupState);
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq("Foo"), Mockito.any());
assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq(ReaderGroupImpl.SILENT + "Bar"), Mockito.any());
order.verify(groupState).getCheckpoint();
order.verify(groupState).checkpoint(Mockito.eq("Baz"), Mockito.any());
order.verify(groupState).findSegmentToReleaseIfRequired();
order.verify(groupState).getCheckpoint();
reader.close();
}
Aggregations