use of io.pravega.client.segment.impl.SegmentOutputStream 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();
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamReaderTest method testCheckpoint.
@Test(timeout = 10000)
public void testCheckpoint() 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()).thenReturn("Foo").thenReturn(null);
EventRead<byte[]> eventRead = reader.readNextEvent(0);
assertTrue(eventRead.isCheckpoint());
assertNull(eventRead.getEvent());
assertEquals("Foo", eventRead.getCheckpointName());
assertEquals(buffer, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
assertNull(reader.readNextEvent(0).getEvent());
reader.close();
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class EventStreamWriterTest method testFailOnClose.
@Test
public void testFailOnClose() throws SegmentSealedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment = new Segment(scope, streamName, 0);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, new JavaSerializer<>(), config, new InlineExecutor());
Mockito.doThrow(new RuntimeException("Intentional exception")).when(outputStream).close();
writer.writeEvent("Foo");
writer.writeEvent("Bar");
try {
writer.close();
fail();
} catch (RuntimeException e) {
// expected.
}
try {
writer.writeEvent("fail");
fail();
} catch (IllegalStateException e) {
// expected
}
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class SegmentTransactionTest method testFlush.
@Test(timeout = 5000)
public void testFlush() throws TxnFailedException, SegmentSealedException {
UUID uuid = UUID.randomUUID();
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
PendingEvent event = (PendingEvent) invocation.getArgument(0);
event.getAckFuture().complete(true);
return null;
}
}).when(outputStream).write(Mockito.any(PendingEvent.class));
txn.writeEvent("hi");
verify(outputStream).write(Mockito.any(PendingEvent.class));
txn.flush();
verify(outputStream).flush();
Mockito.verifyNoMoreInteractions(outputStream);
}
use of io.pravega.client.segment.impl.SegmentOutputStream in project pravega by pravega.
the class SegmentTransactionTest method testSegmentDoesNotExist.
@Test(timeout = 5000)
public void testSegmentDoesNotExist() {
UUID uuid = UUID.randomUUID();
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
PendingEvent event = (PendingEvent) invocation.getArgument(0);
event.getAckFuture().completeExceptionally(new NoSuchSegmentException("segment"));
return null;
}
}).when(outputStream).write(Mockito.any(PendingEvent.class));
AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.writeEvent("hi"));
verify(outputStream).write(Mockito.any(PendingEvent.class));
AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.flush());
Mockito.verifyNoMoreInteractions(outputStream);
}
Aggregations