@Test public void constructors_throw_exception_if_passed_null_consumer() { // given Deque<Span> spanStackMock = mock(Deque.class); Map<String, String> mdcInfoMock = mock(Map.class); // expect assertThat(catchThrowable(() -> new ChannelFutureListenerWithTracingAndMdc(null, ctxMock))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new ChannelFutureListenerWithTracingAndMdc(null, Pair.of(spanStackMock, mdcInfoMock)))) .isInstanceOf(IllegalArgumentException.class); // and expect assertThat(catchThrowable(() -> new ChannelFutureListenerWithTracingAndMdc(null, spanStackMock, mdcInfoMock))) .isInstanceOf(IllegalArgumentException.class); }
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); ChannelFutureListenerWithTracingAndMdc instance = new ChannelFutureListenerWithTracingAndMdc( consumerMock, spanStack, mdcInfo ); Throwable ex = catchThrowable(() -> instance.operationComplete(inObj));
protected void endDtrace(ChannelHandlerContext ctx) { HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); // Due to multiple messages and exception possibilities/interactions it's possible we've already ended the trace // for this request, so make sure we only complete it if appropriate. if (httpProcessingState == null) { // How did we get here?? Something major blew up. Oh well, attempt to complete the trace. runnableWithTracingAndMdc( this::completeCurrentSpan, ctx ).run(); } else if (!httpProcessingState.isTraceCompletedOrScheduled()) { // We have a state and the trace has not been completed yet. If there was no response sent then complete the // trace now (should only happen under rare error conditions), otherwise complete it when the response // finishes. In either case we perform response tagging and final span name if it hasn't already // been done (which it should be, but just in case...) httpProcessingState.setTraceCompletedOrScheduled(true); httpProcessingState.handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone(); if (!httpProcessingState.isResponseSendingLastChunkSent()) { runnableWithTracingAndMdc( this::completeCurrentSpan, ctx ).run(); } else { httpProcessingState.getResponseWriterFinalChunkChannelFuture().addListener( new ChannelFutureListenerWithTracingAndMdc(postResponseSentOperation, ctx)); } } }
@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 ChannelFutureListenerWithTracingAndMdc instance = new ChannelFutureListenerWithTracingAndMdc( consumerMock, spanStackMock, mdcInfoMock ); // then assertThat(instance.postCompleteOperation).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
protected void endDtrace(ChannelHandlerContext ctx) { HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); // Due to multiple messages and exception possibilities/interactions it's possible we've already ended the trace // for this request, so make sure we only complete it if appropriate. if (httpProcessingState == null) { // How did we get here?? Something major blew up. Oh well, attempt to complete the trace. runnableWithTracingAndMdc( this::completeCurrentSpan, ctx ).run(); } else if (!httpProcessingState.isTraceCompletedOrScheduled()) { // We have a state and the trace has not been completed yet. If there was no response sent then complete the // trace now (should only happen under rare error conditions), otherwise complete it when the response // finishes. In either case we perform response tagging and final span name if it hasn't already // been done (which it should be, but just in case...) httpProcessingState.setTraceCompletedOrScheduled(true); httpProcessingState.handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone(); if (!httpProcessingState.isResponseSendingLastChunkSent()) { runnableWithTracingAndMdc( this::completeCurrentSpan, ctx ).run(); } else { httpProcessingState.getResponseWriterFinalChunkChannelFuture().addListener( new ChannelFutureListenerWithTracingAndMdc(postResponseSentOperation, ctx)); } } }
@Test public void pair_constructor_sets_fields_as_expected_when_pair_is_null() { // when ChannelFutureListenerWithTracingAndMdc instance = new ChannelFutureListenerWithTracingAndMdc(consumerMock, (Pair)null); // then assertThat(instance.postCompleteOperation).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 ChannelFutureListenerWithTracingAndMdc instance = new ChannelFutureListenerWithTracingAndMdc(consumerMock, ctxMock); // then assertThat(instance.postCompleteOperation).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 ChannelFutureListenerWithTracingAndMdc instance = new ChannelFutureListenerWithTracingAndMdc( consumerMock, Pair.of(spanStackMock, mdcInfoMock) ); // then assertThat(instance.postCompleteOperation).isSameAs(consumerMock); assertThat(instance.distributedTraceStackForExecution).isEqualTo(spanStackMock); assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock); }
RequestInfo requestInfo = httpProcessingState.getRequestInfo(); ChannelFutureListener doTheAccessLoggingOperation = new ChannelFutureListenerWithTracingAndMdc( (channelFuture) -> accessLogger.log( requestInfo, actualResponseObject, responseInfo,
RequestInfo requestInfo = httpProcessingState.getRequestInfo(); ChannelFutureListener doTheAccessLoggingOperation = new ChannelFutureListenerWithTracingAndMdc( (channelFuture) -> accessLogger.log( requestInfo, actualResponseObject, responseInfo,