Search in sources :

Example 91 with Consumer

use of java.util.function.Consumer in project riposte by Nike-Inc.

the class AsyncNettyHelperTest method consumerWithTracingAndMdc_ctx_works_as_expected.

@Test
public void consumerWithTracingAndMdc_ctx_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = setupStateWithTracingAndMdcInfo();
    // when
    Consumer result = AsyncNettyHelper.consumerWithTracingAndMdc(consumerMock, ctxMock);
    // then
    verifyConsumerWithTracingAndMdcSupport(result, consumerMock, setupInfo.getLeft(), setupInfo.getRight());
}
Also used : BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) Deque(java.util.Deque) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 92 with Consumer

use of java.util.function.Consumer in project pravega by pravega.

the class ClientFactoryImpl method createRevisionedStreamClient.

@Override
public <T> RevisionedStreamClient<T> createRevisionedStreamClient(String streamName, Serializer<T> serializer, SynchronizerConfig config) {
    log.info("Creating revisioned stream client for stream: {} with synchronizer configuration: {}", streamName, config);
    Segment segment = new Segment(scope, streamName, 0);
    SegmentInputStream in = inFactory.createInputStreamForSegment(segment);
    // Segment sealed is not expected for Revisioned Stream Client.
    Consumer<Segment> segmentSealedCallBack = s -> {
        throw new IllegalStateException("RevisionedClient: Segmentsealed exception observed for segment:" + s);
    };
    String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStreamName()), RuntimeException::new);
    SegmentOutputStream out = outFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config.getEventWriterConfig(), delegationToken);
    SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationToken);
    return new RevisionedStreamClientImpl<>(segment, in, out, meta, serializer, controller, delegationToken);
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) Segment(io.pravega.client.segment.impl.Segment) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) Supplier(java.util.function.Supplier) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) Update(io.pravega.client.state.Update) Stream(io.pravega.client.stream.Stream) RevisionedStreamClient(io.pravega.client.state.RevisionedStreamClient) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) BatchClientImpl(io.pravega.client.batch.impl.BatchClientImpl) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) StateSynchronizerImpl(io.pravega.client.state.impl.StateSynchronizerImpl) lombok.val(lombok.val) EventStreamReader(io.pravega.client.stream.EventStreamReader) InitialUpdate(io.pravega.client.state.InitialUpdate) Revisioned(io.pravega.client.state.Revisioned) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) UpdateOrInitSerializer(io.pravega.client.state.impl.UpdateOrInitSerializer) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ClientFactory(io.pravega.client.ClientFactory) BatchClient(io.pravega.client.batch.BatchClient) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ReaderConfig(io.pravega.client.stream.ReaderConfig) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Futures(io.pravega.common.concurrent.Futures) ClientConfig(io.pravega.client.ClientConfig) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient)

Example 93 with Consumer

use of java.util.function.Consumer in project pravega by pravega.

the class DataFrameInputStreamTests method toDataFrames.

private ArrayList<LogItem> toDataFrames(ArrayList<TestItem> items) throws Exception {
    val result = new ArrayList<LogItem>(RECORD_COUNT);
    Consumer<DataFrame> addCallback = df -> result.add(new LogItem(df.getData().getReader(), df.getData().getLength(), new TestLogAddress(result.size())));
    try (val dos = new DataFrameOutputStream(FRAME_SIZE, addCallback)) {
        for (TestItem item : items) {
            dos.startNewRecord();
            long beginSequence = result.size();
            dos.write(item.data);
            dos.endRecord();
            item.address = new TestLogAddress(result.size());
            for (long s = beginSequence; s <= item.address.getSequence(); s++) {
                item.dataFrames.add(s);
            }
        }
        dos.flush();
    }
    return result;
}
Also used : lombok.val(lombok.val) CloseableIterator(io.pravega.common.util.CloseableIterator) DurableDataLog(io.pravega.segmentstore.storage.DurableDataLog) AssertExtensions(io.pravega.test.common.AssertExtensions) Collection(java.util.Collection) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) Test(org.junit.Test) LogAddress(io.pravega.segmentstore.storage.LogAddress) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) Data(lombok.Data) DurableDataLogException(io.pravega.segmentstore.storage.DurableDataLogException) Preconditions(com.google.common.base.Preconditions) Assert(org.junit.Assert) Collections(java.util.Collections) StreamHelpers(io.pravega.common.io.StreamHelpers) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Example 94 with Consumer

use of java.util.function.Consumer in project pravega by pravega.

the class Futures method doWhileLoop.

/**
 * Executes a code fragment returning a CompletableFutures while a condition on the returned value is satisfied.
 *
 * @param condition Predicate that indicates whether to proceed with the loop or not.
 * @param loopBody  A Supplier that returns a CompletableFuture which represents the body of the loop. This
 *                  supplier is invoked every time the loopBody needs to execute.
 * @param executor  An Executor that is used to execute the condition and the loop support code.
 * @param <T>       Return type of the executor.
 * @return A CompletableFuture that, when completed, indicates the loop terminated without any exception. If
 * either the loopBody or condition throw/return Exceptions, these will be set as the result of this returned Future.
 */
public static <T> CompletableFuture<Void> doWhileLoop(Supplier<CompletableFuture<T>> loopBody, Predicate<T> condition, Executor executor) {
    CompletableFuture<Void> result = new CompletableFuture<>();
    // We implement the do-while loop using a regular loop, but we execute one iteration before we create the actual Loop object.
    // Since this method has slightly different arguments than loop(), we need to make one adjustment:
    // * After each iteration, we get the result and run it through 'condition' and use that to decide whether to continue.
    AtomicBoolean canContinue = new AtomicBoolean();
    Consumer<T> iterationResultHandler = ir -> canContinue.set(condition.test(ir));
    loopBody.get().thenAccept(iterationResultHandler).thenRunAsync(() -> {
        Loop<T> loop = new Loop<>(canContinue::get, loopBody, iterationResultHandler, result, executor);
        executor.execute(loop);
    }, executor).exceptionally(ex -> {
        // Handle exceptions from the first iteration.
        result.completeExceptionally(ex);
        return null;
    });
    return result;
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) SneakyThrows(lombok.SneakyThrows) Exceptions(io.pravega.common.Exceptions) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Future(java.util.concurrent.Future) Duration(java.time.Duration) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Callbacks(io.pravega.common.function.Callbacks) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Collection(java.util.Collection) lombok.val(lombok.val) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) Data(lombok.Data) Preconditions(com.google.common.base.Preconditions) Lombok(lombok.Lombok) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 95 with Consumer

use of java.util.function.Consumer in project pravega by pravega.

the class CallbacksTests method testInvokeConsumer.

/**
 * Tests the CallbackHelpers.invokeSafely(Consumer) method.
 */
@Test
public void testInvokeConsumer() {
    // Test happy case: no exception.
    AtomicInteger invokeCount = new AtomicInteger(0);
    Consumer<Integer> goodConsumer = v -> invokeCount.incrementAndGet();
    AtomicBoolean exceptionHandled = new AtomicBoolean(false);
    Consumer<Throwable> failureHandler = ex -> exceptionHandled.set(true);
    Callbacks.invokeSafely(goodConsumer, 1, failureHandler);
    Assert.assertFalse("Exception handler was invoked when no exception was thrown.", exceptionHandled.get());
    Assert.assertEquals("Unexpected number of callback invocations.", 1, invokeCount.get());
    invokeCount.set(0);
    exceptionHandled.set(false);
    // Test exceptional case
    Consumer<Integer> badConsumer = v -> {
        throw new RuntimeException("intentional");
    };
    // With explicit callback.
    Callbacks.invokeSafely(badConsumer, 1, failureHandler);
    Assert.assertTrue("Exception handler was not invoked when an exception was thrown.", exceptionHandled.get());
    Assert.assertEquals("Unexpected number of callback invocations.", 0, invokeCount.get());
    exceptionHandled.set(false);
    // With no callback.
    Callbacks.invokeSafely(badConsumer, 1, null);
    Assert.assertEquals("Unexpected number of callback invocations.", 0, invokeCount.get());
    // With callback that throws exceptions.
    Callbacks.invokeSafely(badConsumer, 1, ex -> {
        throw new IllegalArgumentException("intentional");
    });
    Assert.assertEquals("Unexpected number of callback invocations.", 0, invokeCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Assert(org.junit.Assert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Consumer (java.util.function.Consumer)908 List (java.util.List)445 ArrayList (java.util.ArrayList)288 Test (org.junit.Test)250 Map (java.util.Map)228 IOException (java.io.IOException)223 Collectors (java.util.stream.Collectors)205 Collections (java.util.Collections)185 Arrays (java.util.Arrays)181 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)163 TimeUnit (java.util.concurrent.TimeUnit)157 HashMap (java.util.HashMap)152 Set (java.util.Set)149 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)146 Optional (java.util.Optional)132 Collection (java.util.Collection)127 Function (java.util.function.Function)119 CountDownLatch (java.util.concurrent.CountDownLatch)116 File (java.io.File)112 AtomicReference (java.util.concurrent.atomic.AtomicReference)111