use of io.pravega.client.segment.impl.SegmentOutputStreamFactory in project pravega by pravega.
the class EventStreamWriterTest method testSegmentSealedInClose.
@Test
public void testSegmentSealedInClose() throws EndOfSegmentException, SegmentTruncatedException {
String scope = "scope";
String streamName = "stream";
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);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream1.callBackForSealed = i.getArgument(1);
return outputStream1;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
writer.writeEvent("Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertTrue(outputStream1.getUnackedEventsOnSeal().size() > 0);
MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
outputStream1.invokeSealedCallBack();
writer.close();
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertTrue(outputStream2.fetchCurrentSegmentLength() > 0);
assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
use of io.pravega.client.segment.impl.SegmentOutputStreamFactory in project pravega by pravega.
the class EventStreamWriterTest method testEndOfSegmentBackgroundRefresh.
@Test
public void testEndOfSegmentBackgroundRefresh() {
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, controller, streamFactory, serializer, config, new InlineExecutor());
writer.writeEvent(routingKey, "Foo");
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment2));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
writer.writeEvent(routingKey, "Bar");
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertEquals(2, outputStream1.getUnackedEventsOnSeal().size());
assertEquals("Foo", serializer.deserialize(outputStream1.getUnackedEventsOnSeal().get(0).getData()));
assertEquals("Bar", serializer.deserialize(outputStream1.getUnackedEventsOnSeal().get(1).getData()));
// simulate a segment sealed callback.
outputStream1.invokeSealedCallBack();
writer.writeEvent(routingKey, "TestData");
// This time the actual handleLogSealed is invoked and the resend method resends data to outputStream2.
assertEquals(3, outputStream2.getUnackedEventsOnSeal().size());
assertEquals("Foo", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(0).getData()));
assertEquals("Bar", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(1).getData()));
assertEquals("TestData", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(2).getData()));
}
use of io.pravega.client.segment.impl.SegmentOutputStreamFactory in project pravega by pravega.
the class EventStreamWriterTest method testRetryFlushSegmentSealed.
@Test
public void testRetryFlushSegmentSealed() throws EndOfSegmentException, SegmentTruncatedException {
String scope = "scope";
String streamName = "stream";
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);
SealedSegmentOutputStream outputStream = new SealedSegmentOutputStream(segment1);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream.callBackForSealed = i.getArgument(1);
return outputStream;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
writer.writeEvent("Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
Async.testBlocking(() -> {
// blocking on flush.
writer.flush();
}, () -> {
// trigger release with a segmentSealedException.
outputStream.releaseFlush();
// trigger Sealed Segment call back.
outputStream.invokeSealedCallBack();
});
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertTrue(outputStream2.fetchCurrentSegmentLength() > 0);
assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
use of io.pravega.client.segment.impl.SegmentOutputStreamFactory in project pravega by pravega.
the class SegmentSelectorTest method testNullRoutingKey.
@Test
public void testNullRoutingKey() {
Controller controller = Mockito.mock(Controller.class);
SegmentOutputStreamFactory factory = Mockito.mock(SegmentOutputStreamFactory.class);
SegmentSelector selector = new SegmentSelector(new StreamImpl(scope, streamName), controller, factory, config);
TreeMap<Double, Segment> segments = new TreeMap<>();
segments.put(0.25, new Segment(scope, streamName, 0));
segments.put(0.5, new Segment(scope, streamName, 1));
segments.put(0.75, new Segment(scope, streamName, 2));
segments.put(1.0, new Segment(scope, streamName, 3));
StreamSegments streamSegments = new StreamSegments(segments, "");
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(CompletableFuture.completedFuture(streamSegments));
selector.refreshSegmentEventWriters(segmentSealedCallback);
int[] counts = new int[4];
Arrays.fill(counts, 0);
for (int i = 0; i < 100; i++) {
Segment segment = selector.getSegmentForEvent(null);
assertNotNull(segment);
counts[segment.getSegmentNumber()]++;
}
for (int count : counts) {
assertTrue(count > 1);
}
}
use of io.pravega.client.segment.impl.SegmentOutputStreamFactory in project pravega by pravega.
the class SegmentSelectorTest method testUsesAllSegments.
@Test
public void testUsesAllSegments() {
Controller controller = Mockito.mock(Controller.class);
SegmentOutputStreamFactory factory = Mockito.mock(SegmentOutputStreamFactory.class);
SegmentSelector selector = new SegmentSelector(new StreamImpl(scope, streamName), controller, factory, config);
TreeMap<Double, Segment> segments = new TreeMap<>();
segments.put(0.25, new Segment(scope, streamName, 0));
segments.put(0.5, new Segment(scope, streamName, 1));
segments.put(0.75, new Segment(scope, streamName, 2));
segments.put(1.0, new Segment(scope, streamName, 3));
StreamSegments streamSegments = new StreamSegments(segments, "");
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(CompletableFuture.completedFuture(streamSegments));
selector.refreshSegmentEventWriters(segmentSealedCallback);
int[] counts = new int[4];
Arrays.fill(counts, 0);
for (int i = 0; i < 20; i++) {
Segment segment = selector.getSegmentForEvent("" + i);
assertNotNull(segment);
counts[segment.getSegmentNumber()]++;
}
for (int count : counts) {
assertTrue(count > 1);
}
}
Aggregations