ThreadLocalScope implements Scope { @Override public void close() { if (scopeManager.tlsScope.get() != this) { return; } scopeManager.tlsScope.set(toRestore); } ThreadLocalScope(ThreadLocalScopeManager scopeManager, Span wrapped); @Override void close();  }
@Test public void implicitSpanStack() throws Exception { Span backgroundSpan = mock(Span.class); Span foregroundSpan = mock(Span.class); Scope backgroundActive = scopeManager.activate(backgroundSpan); try { assertNotNull(backgroundActive); assertEquals(scopeManager.activeSpan(), backgroundSpan); Scope foregroundActive = scopeManager.activate(foregroundSpan); try { assertNotNull(foregroundActive); assertEquals(scopeManager.activeSpan(), foregroundSpan); } finally { foregroundActive.close(); } assertEquals(scopeManager.activeSpan(), backgroundSpan); } finally { backgroundActive.close(); } verify(backgroundSpan, times(0)).finish(); verify(foregroundSpan, times(0)).finish(); assertNull(scopeManager.activeSpan()); }
@Test public void testDeactivateWhenDifferentSpanIsActive() { Span span = mock(Span.class); Scope active = scopeManager.activate(span); scopeManager.activate(mock(Span.class)); active.close(); verify(span, times(0)).finish(); }