use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class EventStreamWriterTest method testSealInvokesFlush.
@Test
public void testSealInvokesFlush() {
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);
FakeSegmentOutputStream outputStream2 = new FakeSegmentOutputStream(segment2);
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;
});
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> {
outputStream2.callBackForSealed = i.getArgument(1);
return outputStream2;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
writer.writeEvent("Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertEquals(1, outputStream1.unacked.size());
assertEquals(0, outputStream1.acked.size());
outputStream1.invokeSealedCallBack();
assertEquals(0, outputStream2.unacked.size());
assertEquals(2, outputStream2.acked.size());
}
use of io.pravega.client.control.impl.Controller 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, "id", controller, streamFactory, new JavaSerializer<>(), config, executorService(), executorService(), null);
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.control.impl.Controller in project pravega by pravega.
the class EventStreamWriterTest method testSegmentSealedInFlush.
@Test
public void testSegmentSealedInFlush() 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 outputStream = 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 -> {
outputStream.callBackForSealed = i.getArgument(1);
return outputStream;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
writer.writeEvent("Foo");
Mockito.verify(controller).getCurrentSegments(any(), any());
assertTrue(outputStream.unacked.size() > 0);
MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2, null);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
outputStream.invokeSealedCallBack();
writer.flush();
Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
assertTrue(outputStream2.fetchCurrentSegmentLength().join() > 0);
assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class EventStreamWriterTest method testWrite.
@Test
public void testWrite() {
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));
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.writeEvent("Foo");
writer.writeEvent("Bar");
writer.close();
try {
writer.writeEvent("fail");
fail();
} catch (IllegalStateException e) {
// expected.
}
}
use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class EventStreamWriterTest method testNoNextSegmentMidClose.
@Test
public void testNoNextSegmentMidClose() {
String scope = "scope";
String streamName = "stream";
String routingKey = "RoutingKey";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment1 = new Segment(scope, streamName, 0);
EventWriterConfig config = EventWriterConfig.builder().build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Controller controller = Mockito.mock(Controller.class);
FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
outputStream1.callBackForSealed = i.getArgument(1);
return outputStream1;
});
JavaSerializer<String> serializer = new JavaSerializer<>();
val empty = CompletableFuture.completedFuture(new StreamSegments(new TreeMap<>()));
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1)).thenReturn(empty);
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
CompletableFuture<Void> future = writer.writeEvent(routingKey, "Foo");
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(empty);
val noFutures = CompletableFuture.completedFuture(new StreamSegmentsWithPredecessors(new HashMap<>(), ""));
Mockito.when(controller.getSuccessors(segment1)).thenReturn(noFutures);
// invoke the sealed callback invocation simulating a netty call back with segment sealed exception.
outputStream1.sealed = true;
assertBlocks(() -> writer.close(), () -> outputStream1.invokeSealedCallBack());
assertTrue(future.isDone());
assertTrue(future.isCompletedExceptionally());
}
Aggregations