use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class EventStreamWriterTest method testAutoNoteTime.
@Test
public void testAutoNoteTime() {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment1 = new Segment(scope, streamName, 0);
EventWriterConfig config = EventWriterConfig.builder().automaticallyNoteTime(true).build();
SegmentOutputStream mockOutputStream = Mockito.mock(SegmentOutputStream.class);
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenReturn(mockOutputStream);
Controller controller = Mockito.mock(Controller.class);
Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
Mockito.when(mockOutputStream.getLastObservedWriteOffset()).thenReturn(1111L);
CollectingExecutor executor = new CollectingExecutor();
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executor, executor, null);
List<Runnable> tasks = executor.getScheduledTasks();
assertEquals(1, tasks.size());
tasks.get(0).run();
ImmutableMap<Segment, Long> expectedOffsets = ImmutableMap.of(segment1, 1111L);
Mockito.verify(controller).noteTimestampFromWriter(eq("id"), eq(stream), Mockito.anyLong(), eq(new WriterPosition(expectedOffsets)));
}
use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class EventStreamWriterTest method testNoNextSegment.
@Test
public void testNoNextSegment() {
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);
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.flush(), () -> outputStream1.invokeSealedCallBack());
assertThrows("Stream should be sealed", () -> writer.writeEvent(routingKey, "Bar"), e -> e.getMessage().contains("sealed"));
writer.close();
}
use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class PingerTest method startTxnKeepAliveError.
@Test
public void startTxnKeepAliveError() throws Exception {
final UUID txnID = UUID.randomUUID();
CompletableFuture<Transaction.PingStatus> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(new RuntimeException("Error"));
when(controller.pingTransaction(eq(stream), eq(txnID), anyLong())).thenReturn(failedFuture);
@Cleanup Pinger pinger = new Pinger(config.getTransactionTimeoutTime(), stream, controller, executor);
pinger.startPing(txnID);
verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), anyLong(), longThat(l -> l > 0 && l <= 50000), eq(TimeUnit.MILLISECONDS));
verify(controller, times(1)).pingTransaction(eq(stream), eq(txnID), eq(config.getTransactionTimeoutTime()));
}
use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class PingerTest method startTxnKeepAliveMultiple.
@Test
public void startTxnKeepAliveMultiple() throws Exception {
final UUID txnID1 = UUID.randomUUID();
final UUID txnID2 = UUID.randomUUID();
@Cleanup Pinger pinger = new Pinger(config.getTransactionTimeoutTime(), stream, controller, executor);
pinger.startPing(txnID1);
pinger.startPing(txnID2);
verify(executor, times(1)).scheduleAtFixedRate(any(Runnable.class), anyLong(), longThat(l -> l > 0 && l <= 50000), eq(TimeUnit.MILLISECONDS));
}
use of io.pravega.client.control.impl.Controller in project pravega by pravega.
the class SegmentSelectorTest method testSealedStream.
@Test
public void testSealedStream() {
final Segment segment0 = new Segment(scope, streamName, 0);
final Segment segment1 = new Segment(scope, streamName, 1);
final CompletableFuture<Void> writerFuture = new CompletableFuture<>();
// Setup Mock.
SegmentOutputStream s0Writer = Mockito.mock(SegmentOutputStream.class);
SegmentOutputStream s1Writer = Mockito.mock(SegmentOutputStream.class);
when(s0Writer.getUnackedEventsOnSeal()).thenReturn(ImmutableList.of(PendingEvent.withHeader("0", ByteBuffer.wrap("e".getBytes()), writerFuture)));
SegmentOutputStreamFactory factory = Mockito.mock(SegmentOutputStreamFactory.class);
when(factory.createOutputStreamForSegment(eq(segment0), ArgumentMatchers.<Consumer<Segment>>any(), any(EventWriterConfig.class), any(DelegationTokenProvider.class))).thenReturn(s0Writer);
when(factory.createOutputStreamForSegment(eq(segment1), ArgumentMatchers.<Consumer<Segment>>any(), any(EventWriterConfig.class), any(DelegationTokenProvider.class))).thenReturn(s1Writer);
Controller controller = Mockito.mock(Controller.class);
SegmentSelector selector = new SegmentSelector(new StreamImpl(scope, streamName), controller, factory, config, DelegationTokenProviderFactory.createWithEmptyToken());
TreeMap<Double, SegmentWithRange> segments = new TreeMap<>();
addNewSegment(segments, 0, 0.0, 0.5);
addNewSegment(segments, 1, 0.5, 1.0);
StreamSegments streamSegments = new StreamSegments(segments);
when(controller.getCurrentSegments(scope, streamName)).thenReturn(CompletableFuture.completedFuture(streamSegments));
// trigger refresh.
selector.refreshSegmentEventWriters(segmentSealedCallback);
// simulate controller returning empty result since the stream is sealed..
when(controller.getSuccessors(segment0)).thenReturn(CompletableFuture.completedFuture(new StreamSegmentsWithPredecessors(Collections.emptyMap(), "")));
assertEquals(Collections.emptyList(), selector.refreshSegmentEventWritersUponSealed(segment0, segmentSealedCallback));
assertFutureThrows("Writer Future", writerFuture, t -> t instanceof IllegalStateException);
}
Aggregations