/** * @return A {@link BiConsumer} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static <T, U> BiConsumer<T, U> biConsumerWithTracingAndMdc(BiConsumer<T, U> biConsumer, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new BiConsumerWithTracingAndMdcSupport<>(biConsumer, distributedTraceStackToLink, mdcContextMapToLink); }
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); BiConsumerWithTracingAndMdcSupport instance = new BiConsumerWithTracingAndMdcSupport( consumerMock, spanStack, mdcInfo ); Throwable ex = catchThrowable(() -> instance.accept(inObj1, inObj2));
/** * @return A {@link BiConsumer} that wraps the given original so that the given {@link ChannelHandlerContext}'s * distributed tracing and MDC information is registered with the thread and therefore available during execution * and unregistered after execution. */ public static <T, U> BiConsumer<T, U> biConsumerWithTracingAndMdc(BiConsumer<T, U> biConsumer, ChannelHandlerContext ctx) { return new BiConsumerWithTracingAndMdcSupport<>(biConsumer, ctx); }
/** * @return A {@link BiConsumer} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static <T, U> BiConsumer<T, U> biConsumerWithTracingAndMdc( BiConsumer<T, U> biConsumer, Pair<Deque<Span>, Map<String, String>> threadInfoToLink ) { return new BiConsumerWithTracingAndMdcSupport<>(biConsumer, threadInfoToLink); }
/** * @return A {@link BiConsumer} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static <T, U> BiConsumer<T, U> biConsumerWithTracingAndMdc(BiConsumer<T, U> biConsumer, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink) { return new BiConsumerWithTracingAndMdcSupport<>(biConsumer, distributedTraceStackToLink, mdcContextMapToLink); }
/** * @return A {@link BiConsumer} that wraps the given original so that the given {@link ChannelHandlerContext}'s * distributed tracing and MDC information is registered with the thread and therefore available during execution * and unregistered after execution. */ public static <T, U> BiConsumer<T, U> biConsumerWithTracingAndMdc(BiConsumer<T, U> biConsumer, ChannelHandlerContext ctx) { return new BiConsumerWithTracingAndMdcSupport<>(biConsumer, ctx); }
/** * @return A {@link BiConsumer} that wraps the given original so that the given distributed tracing and MDC * information is registered with the thread and therefore available during execution and unregistered after * execution. */ public static <T, U> BiConsumer<T, U> biConsumerWithTracingAndMdc( BiConsumer<T, U> biConsumer, Pair<Deque<Span>, Map<String, String>> threadInfoToLink ) { return new BiConsumerWithTracingAndMdcSupport<>(biConsumer, threadInfoToLink); }
@Test public void constructors_throw_exception_if_passed_null_biconsumer() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // expect assertThat(catchThrowable(() -> new BiConsumerWithTracingAndMdcSupport(null, ctxMock))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new BiConsumerWithTracingAndMdcSupport(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new BiConsumerWithTracingAndMdcSupport(null, spanStackMock, mdcInfoMock))) .isInstanceOf(IllegalArgumentException.class); }
@Test public void kitchen_sink_constructor_sets_fields_as_expected() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // when BiConsumerWithTracingAndMdcSupport instance = new BiConsumerWithTracingAndMdcSupport( consumerMock, spanStackMock, mdcInfoMock ); // then assertThat(instance.origBiConsumer).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null() { // when BiConsumerWithTracingAndMdcSupport instance = new BiConsumerWithTracingAndMdcSupport(consumerMock, (Pair)null); // then assertThat(instance.origBiConsumer).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isNull(); assertThat(instance.mdcContextMapForExecution).isNull(); }
@Test public void ctx_constructor_sets_fields_as_expected() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); state.setDistributedTraceStack(spanStackMock); state.setLoggerMdcContextMap(mdcInfoMock); // when BiConsumerWithTracingAndMdcSupport instance = new BiConsumerWithTracingAndMdcSupport(consumerMock, ctxMock); // then assertThat(instance.origBiConsumer).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
@DataProvider(value = { "true | true", "true | false", "false | true", "false | false", }, splitBy = "\\|") @Test public void pair_constructor_sets_fields_as_expected(boolean nullSpanStack, boolean nullMdcInfo) { // given Deque<Span> spanStackMock = (nullSpanStack) ? null : mock(Deque.class); Map<String, String> mdcInfoMock = (nullMdcInfo) ? null : mock(Map.class); // when BiConsumerWithTracingAndMdcSupport instance = new BiConsumerWithTracingAndMdcSupport( consumerMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.origBiConsumer).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }