Search in sources :

Example 86 with Controller

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)));
}
Also used : Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) CollectingExecutor(io.pravega.test.common.CollectingExecutor) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 87 with Controller

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();
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) Controller(io.pravega.client.control.impl.Controller) TreeMap(java.util.TreeMap) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Test(org.junit.Test)

Example 88 with Controller

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()));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ScheduledFuture(java.util.concurrent.ScheduledFuture) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.timeout(org.mockito.Mockito.timeout) Stream(io.pravega.client.stream.Stream) After(org.junit.After) Spy(org.mockito.Spy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Status(io.grpc.Status) Transaction(io.pravega.client.stream.Transaction) Before(org.junit.Before) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) InlineExecutor(io.pravega.test.common.InlineExecutor) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.longThat(org.mockito.ArgumentMatchers.longThat) CompletableFuture(java.util.concurrent.CompletableFuture) StatusRuntimeException(io.grpc.StatusRuntimeException) UUID(java.util.UUID) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 89 with Controller

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));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ScheduledFuture(java.util.concurrent.ScheduledFuture) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.timeout(org.mockito.Mockito.timeout) Stream(io.pravega.client.stream.Stream) After(org.junit.After) Spy(org.mockito.Spy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Status(io.grpc.Status) Transaction(io.pravega.client.stream.Transaction) Before(org.junit.Before) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) InlineExecutor(io.pravega.test.common.InlineExecutor) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.longThat(org.mockito.ArgumentMatchers.longThat) UUID(java.util.UUID) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 90 with Controller

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);
}
Also used : Controller(io.pravega.client.control.impl.Controller) TreeMap(java.util.TreeMap) Segment(io.pravega.client.segment.impl.Segment) CompletableFuture(java.util.concurrent.CompletableFuture) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) Test(org.junit.Test)

Aggregations

Controller (io.pravega.client.control.impl.Controller)120 Test (org.junit.Test)95 Cleanup (lombok.Cleanup)81 Segment (io.pravega.client.segment.impl.Segment)53 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)50 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)47 Stream (io.pravega.client.stream.Stream)37 CompletableFuture (java.util.concurrent.CompletableFuture)35 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)34 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)33 HashMap (java.util.HashMap)29 ClientConfig (io.pravega.client.ClientConfig)28 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)28 StreamImpl (io.pravega.client.stream.impl.StreamImpl)25 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)24 Slf4j (lombok.extern.slf4j.Slf4j)24 Before (org.junit.Before)23 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)22 ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)21 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)21