use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class SupplierWithTracingAndMdcSupportTest method apply_handles_tracing_and_mdc_info_as_expected.
@DataProvider(value = { "true", "false" })
@Test
public void apply_handles_tracing_and_mdc_info_as_expected(boolean throwException) {
// given
throwExceptionDuringCall = throwException;
Tracer.getInstance().startRequestWithRootSpan("foo");
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy();
Map<String, String> mdcInfo = MDC.getCopyOfContextMap();
SupplierWithTracingAndMdcSupport instance = new SupplierWithTracingAndMdcSupport(supplierMock, spanStack, mdcInfo);
resetTracingAndMdc();
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
// when
Throwable ex = null;
Object result = null;
try {
result = instance.get();
} catch (Throwable t) {
ex = t;
}
// then
verify(supplierMock).get();
if (throwException) {
assertThat(ex).isNotNull();
assertThat(result).isNull();
} else {
assertThat(ex).isNull();
assertThat(result).isSameAs(outObj);
}
assertThat(currentSpanStackWhenSupplierWasCalled.get(0)).isEqualTo(spanStack);
assertThat(currentMdcInfoWhenSupplierWasCalled.get(0)).isEqualTo(mdcInfo);
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class RunnableWithTracingAndMdcSupportTest method run_handles_tracing_and_mdc_info_as_expected.
@DataProvider(value = { "true", "false" })
@Test
public void run_handles_tracing_and_mdc_info_as_expected(boolean throwException) {
// given
throwExceptionDuringCall = throwException;
Tracer.getInstance().startRequestWithRootSpan("foo");
Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy();
Map<String, String> mdcInfo = MDC.getCopyOfContextMap();
RunnableWithTracingAndMdcSupport instance = new RunnableWithTracingAndMdcSupport(runnableMock, spanStack, mdcInfo);
resetTracingAndMdc();
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
// when
Throwable ex = catchThrowable(() -> instance.run());
// then
verify(runnableMock).run();
if (throwException)
assertThat(ex).isNotNull();
else
assertThat(ex).isNull();
assertThat(currentSpanStackWhenRunnableWasCalled.get(0)).isEqualTo(spanStack);
assertThat(currentMdcInfoWhenRunnableWasCalled.get(0)).isEqualTo(mdcInfo);
assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull();
assertThat(MDC.getCopyOfContextMap()).isEmpty();
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class DefaultRiposteServerSpanNamingAndTaggingStrategyTest method doChangeSpanName_changes_span_name_as_expected.
@DataProvider(value = { "null | false", " | false", "[whitespace] | false", "fooNewName | true" }, splitBy = "\\|")
@Test
public void doChangeSpanName_changes_span_name_as_expected(String newName, boolean expectNameToBeChanged) {
// given
if ("[whitespace]".equals(newName)) {
newName = " \r\n\t ";
}
String initialSpanName = UUID.randomUUID().toString();
Span span = Span.newBuilder(initialSpanName, Span.SpanPurpose.CLIENT).build();
String expectedSpanName = (expectNameToBeChanged) ? newName : initialSpanName;
// when
impl.doChangeSpanName(span, newName);
// then
assertThat(span.getSpanName()).isEqualTo(expectedSpanName);
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class DTraceEndHandlerTest method endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true.
@Test
public void endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true() throws Exception {
// given
assertThat(state.isTraceCompletedOrScheduled(), is(false));
assertThat(state.isResponseSendingLastChunkSent(), is(true));
assertThat(state.getDistributedTraceStack(), nullValue());
Pair<Deque<Span>, Map<String, String>> expectedDtraceInfo = setupStateWithNewSpan("blahTrace");
assertThat(state.getDistributedTraceStack(), notNullValue());
assertThat(state.getDistributedTraceStack(), is(expectedDtraceInfo.getLeft()));
assertThat(state.getDistributedTraceStack().size(), is(1));
assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(false));
Span expectedSpan = expectedDtraceInfo.getLeft().peek();
// when
handlerSpy.endDtrace(ctxMock);
// then
// completeCurrentSpan() not immediately called, but scheduled
verify(handlerSpy, never()).completeCurrentSpan();
assertThat(state.isTraceCompletedOrScheduled(), is(true));
// Response tagging was done.
assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(true));
// Extract the listener that was attached to the last chunk future.
GenericFutureListener lastChunkListener = extractChannelFutureListenerAddedToLastChunkFuture();
assertThat(lastChunkListener, notNullValue());
assertThat(lastChunkListener, instanceOf(ChannelFutureListenerWithTracingAndMdc.class));
assertThat(Whitebox.getInternalState(lastChunkListener, "distributedTraceStackForExecution"), is(expectedDtraceInfo.getLeft()));
assertThat(Whitebox.getInternalState(lastChunkListener, "mdcContextMapForExecution"), is(expectedDtraceInfo.getRight()));
Consumer<ChannelFuture> embeddedListenerConsumer = (Consumer<ChannelFuture>) Whitebox.getInternalState(lastChunkListener, "postCompleteOperation");
// Execute the embedded listener so we can validate what it does. Note that we can't verify using mockito spy verify(),
// because the method call goes through the internal handler, not the spy impl. But we can still verify by
// setting up the Tracer state to what we expect, execute the embedded listener, and verify subsequent Tracer state.
AsyncNettyHelper.linkTracingAndMdcToCurrentThread(expectedDtraceInfo);
assertThat(Tracer.getInstance().getCurrentSpan(), is(expectedSpan));
embeddedListenerConsumer.accept(null);
assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
}
use of com.nike.wingtips.Span in project riposte by Nike-Inc.
the class DTraceEndHandlerTest method endDtrace_completes_the_trace_immediately_if_state_is_not_null_but_isResponseSendingLastChunkSent_returns_false.
@Test
public void endDtrace_completes_the_trace_immediately_if_state_is_not_null_but_isResponseSendingLastChunkSent_returns_false() {
// given
assertThat(state.isTraceCompletedOrScheduled(), is(false));
state.setResponseWriterFinalChunkChannelFuture(null);
assertThat(state.isResponseSendingLastChunkSent(), is(false));
assertThat(state.getDistributedTraceStack(), nullValue());
Pair<Deque<Span>, Map<String, String>> expectedDtraceInfo = setupStateWithNewSpan("blahTrace");
assertThat(state.getDistributedTraceStack(), notNullValue());
assertThat(state.getDistributedTraceStack(), is(expectedDtraceInfo.getLeft()));
assertThat(state.getDistributedTraceStack().size(), is(1));
assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(false));
Span expectedSpan = expectedDtraceInfo.getLeft().peek();
// when
handlerSpy.endDtrace(ctxMock);
// then
verify(handlerSpy).completeCurrentSpan();
assertThat(currentSpanWhenCompleteCurrentSpanWasCalled, is(expectedSpan));
assertThat(currentSpanAfterCompleteCurrentSpanWasCalled, nullValue());
assertThat(state.isTraceCompletedOrScheduled(), is(true));
assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(true));
}
Aggregations