use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.
the class RevisionedStreamClientTest method testSegmentTruncation.
@Test
public void testSegmentTruncation() {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
Revision r0 = client.fetchLatestRevision();
client.writeUnconditionally("a");
Revision ra = client.fetchLatestRevision();
client.writeUnconditionally("b");
Revision rb = client.fetchLatestRevision();
client.writeUnconditionally("c");
Revision rc = client.fetchLatestRevision();
assertEquals(r0, client.fetchOldestRevision());
client.truncateToRevision(r0);
assertEquals(r0, client.fetchOldestRevision());
client.truncateToRevision(ra);
assertEquals(ra, client.fetchOldestRevision());
client.truncateToRevision(r0);
assertEquals(ra, client.fetchOldestRevision());
AssertExtensions.assertThrows(TruncatedDataException.class, () -> client.readFrom(r0));
Iterator<Entry<Revision, String>> iterA = client.readFrom(ra);
assertTrue(iterA.hasNext());
Iterator<Entry<Revision, String>> iterB = client.readFrom(ra);
assertTrue(iterB.hasNext());
assertEquals("b", iterA.next().getValue());
assertEquals("b", iterB.next().getValue());
client.truncateToRevision(rb);
assertTrue(iterA.hasNext());
assertEquals("c", iterA.next().getValue());
client.truncateToRevision(rc);
assertFalse(iterA.hasNext());
assertTrue(iterB.hasNext());
AssertExtensions.assertThrows(TruncatedDataException.class, () -> iterB.next());
}
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, 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, "");
SegmentMetadataClient metadataClient = segmentStreamFactory.createSegmentMetadataClient(segment, "");
ByteBuffer buffer1 = writeInt(stream, 1);
ByteBuffer buffer2 = writeInt(stream, 2);
ByteBuffer buffer3 = writeInt(stream, 3);
long length = metadataClient.fetchCurrentSegmentLength();
assertEquals(0, length % 3);
EventRead<byte[]> event1 = reader.readNextEvent(0);
assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
metadataClient.truncateSegment(segment, length / 3);
assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
metadataClient.truncateSegment(segment, length);
ByteBuffer buffer4 = writeInt(stream, 4);
AssertExtensions.assertThrows(TruncatedDataException.class, () -> reader.readNextEvent(0));
assertEquals(buffer4, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
AssertExtensions.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 testRead.
@Test(timeout = 10000)
public void testRead() 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 buffer1 = writeInt(stream, 1);
ByteBuffer buffer2 = writeInt(stream, 2);
ByteBuffer buffer3 = writeInt(stream, 3);
assertEquals(buffer1, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertEquals(buffer3, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
reader.close();
}
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, 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 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 testRestore.
@Test(timeout = 10000)
public void testRestore() 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);
Mockito.when(groupState.getCheckpoint()).thenThrow(new ReinitializationRequiredException());
try {
reader.readNextEvent(0);
fail();
} catch (ReinitializationRequiredException e) {
// expected
}
assertTrue(reader.getReaders().isEmpty());
reader.close();
}
Aggregations