use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class ReaderGroupImplTest method getUnreadBytesBasedOnLastPosition.
@Test
public void getUnreadBytesBasedOnLastPosition() {
final String stream = "s1";
final StreamCut startStreamCut = getStreamCut(stream, 10L, 1, 2);
final StreamCut endStreamCut = getStreamCut(stream, 30L, 1, 2);
// setup mocks
// simulate zero checkpoints.
when(state.getPositionsForLastCompletedCheckpoint()).thenReturn(Optional.empty());
Map<SegmentWithRange, Long> positions = startStreamCut.asImpl().getPositions().entrySet().stream().collect(Collectors.toMap(e -> new SegmentWithRange(e.getKey(), null), e -> e.getValue()));
when(state.getPositions()).thenReturn(ImmutableMap.of(Stream.of(SCOPE, stream), positions));
when(state.getEndSegments()).thenReturn(endStreamCut.asImpl().getPositions());
when(synchronizer.getState()).thenReturn(state);
ImmutableSet<Segment> r = ImmutableSet.<Segment>builder().addAll(startStreamCut.asImpl().getPositions().keySet()).addAll(endStreamCut.asImpl().getPositions().keySet()).build();
when(controller.getSegments(startStreamCut, endStreamCut)).thenReturn(CompletableFuture.completedFuture(new StreamSegmentSuccessors(r, "")));
assertEquals(40L, readerGroup.unreadBytes());
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class ReaderGroupImplTest method getStreamCut.
private StreamCut getStreamCut(String streamName, long offset, int... segmentNumbers) {
ImmutableMap.Builder<Segment, Long> builder = ImmutableMap.<Segment, Long>builder();
Arrays.stream(segmentNumbers).forEach(seg -> {
builder.put(new Segment(SCOPE, streamName, seg), offset);
});
return new StreamCutImpl(Stream.of(SCOPE, streamName), builder.build());
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class ReaderGroupStateTest method getStreamNames.
@Test
public void getStreamNames() {
// configured Streams.
Set<String> configuredStreams = ImmutableSet.of(getStream("S1").getScopedName(), getStream("S2").getScopedName());
// validate stream names
assertEquals(configuredStreams, readerState.getStreamNames());
// Simulate addition of a reader and assigning of segments to the reader.
new AddReader("reader1").applyTo(readerState, revision);
new ReaderGroupState.AcquireSegment("reader1", new Segment(SCOPE, "S1", 0)).applyTo(readerState, revision);
// validate stream names
assertEquals(configuredStreams, readerState.getStreamNames());
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamWriterTest method testWriteEvents.
@Test
public void testWriteEvents() {
String scope = "scope1";
String streamName = "stream1";
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));
MockSegmentIoStreams outputStream = new MockSegmentIoStreams(segment, null);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, new JavaSerializer<>(), config, executorService(), executorService(), null);
writer.writeEvents("1", Lists.newArrayList("Foo", "Bar")).join();
writer.writeEvent("1", "Foo2").join();
writer.close();
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamWriterTest method testEndOfSegment.
@Test
public void testEndOfSegment() {
String scope = "scope";
String streamName = "stream";
String routingKey = "RoutingKey";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment1 = new Segment(scope, streamName, 0);
Segment segment2 = new Segment(scope, streamName, 1);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
FakeSegmentOutputStream outputStream2 = new FakeSegmentOutputStream(segment2);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream1.callBackForSealed = i.getArgument(1);
return outputStream1;
});
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> {
outputStream2.callBackForSealed = i.getArgument(1);
return outputStream2;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1)).thenReturn(getSegmentsFuture(segment2));
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
writer.writeEvent(routingKey, "Foo");
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment2));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
// invoke the sealed callback invocation simulating a netty call back with segment sealed exception.
outputStream1.invokeSealedCallBack();
writer.writeEvent(routingKey, "Bar");
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertEquals(2, outputStream2.acked.size());
assertEquals(1, outputStream2.unacked.size());
assertEquals("Foo", serializer.deserialize(outputStream2.getAcked(0)));
assertEquals("Bar", serializer.deserialize(outputStream2.getUnacked(0)));
}
Aggregations