From 73ff552a99a15a0d0c1518ebfd6f45d86a203bf8 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Wed, 24 Oct 2018 14:39:49 +0200 Subject: [PATCH] Add Clearable test for OpenTracing propagation Tests must be extended when the following PR is merged: https://github.com/opentracing/opentracing-java/pull/313 --- .../context/opentracing/SpanManager.java | 3 ++- .../context/opentracing/SpanManagerTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/opentracing-span-propagation/src/main/java/nl/talsmasoftware/context/opentracing/SpanManager.java b/opentracing-span-propagation/src/main/java/nl/talsmasoftware/context/opentracing/SpanManager.java index deaca051..ee389e36 100644 --- a/opentracing-span-propagation/src/main/java/nl/talsmasoftware/context/opentracing/SpanManager.java +++ b/opentracing-span-propagation/src/main/java/nl/talsmasoftware/context/opentracing/SpanManager.java @@ -44,7 +44,8 @@ public class SpanManager implements ContextManager { */ @Override public Context getActiveContext() { - return new ManagedSpan(GlobalTracer.get().activeSpan(), null); + Span activeSpan = GlobalTracer.get().activeSpan(); + return activeSpan == null ? null : new ManagedSpan(activeSpan, null); } /** diff --git a/opentracing-span-propagation/src/test/java/nl/talsmasoftware/context/opentracing/SpanManagerTest.java b/opentracing-span-propagation/src/test/java/nl/talsmasoftware/context/opentracing/SpanManagerTest.java index d2f2b62f..41af9ea7 100644 --- a/opentracing-span-propagation/src/test/java/nl/talsmasoftware/context/opentracing/SpanManagerTest.java +++ b/opentracing-span-propagation/src/test/java/nl/talsmasoftware/context/opentracing/SpanManagerTest.java @@ -25,6 +25,7 @@ import io.opentracing.util.ThreadLocalScopeManager; import nl.talsmasoftware.context.Context; import nl.talsmasoftware.context.ContextManager; +import nl.talsmasoftware.context.ContextManagers; import nl.talsmasoftware.context.executors.ContextAwareExecutorService; import org.junit.After; import org.junit.Before; @@ -215,4 +216,15 @@ public void testDeprecatedClassStillWorks() { newContext.close(); newContext.close(); } + + @Test + public void testClearingAllContexts() { + Span span = mockTracer.buildSpan("test-span").start(); + Scope scope = mockTracer.scopeManager().activate(span, false); + assertThat(new SpanManager().getActiveContext().getValue(), is(sameInstance(span))); + + ContextManagers.clearActiveContexts(); + // TODO Test after this is merged: https://github.com/opentracing/opentracing-java/pull/313 +// assertThat(new SpanManager().getActiveContext(), is(nullValue())); + } }