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());
}
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);
}
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;
}
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;
}
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());
}
Aggregations