TracingState extends Pair<Deque<Span>, Map<String, String>> { public @Nullable Span getActiveSpan() { if (spanStack == null) { return null; } return spanStack.peek(); } TracingState(Deque<Span> spanStack, Map<String, String> mdcInfo); static TracingState getCurrentThreadTracingState(); @Override Deque<Span> getLeft(); @Override Map<String, String> getRight(); @Override Map<String, String> setValue(Map<String, String> value); @Nullable Span getActiveSpan(); final Deque<Span> spanStack; final Map<String, String> mdcInfo; }
@Test public void getActiveSpan_works_as_expected() { Deque<Span> stackMock = mock(Deque.class); Span spanMock = mock(Span.class); doReturn(spanMock).when(stackMock).peek(); TracingState tc = new TracingState(stackMock, Collections.emptyMap()); Span result = tc.getActiveSpan(); assertThat(result).isSameAs(spanMock); verify(stackMock).peek(); }
@DataProvider(value = { "true", "false" }) @Test public void getActiveSpan_returns_null_if_stack_is_null_or_empty(boolean stackIsNull) { Deque<Span> stack = (stackIsNull) ? null : new ArrayDeque<>(); TracingState tc = new TracingState(stack, Collections.emptyMap()); Span result = tc.getActiveSpan(); assertThat(result).isNull(); }