From 851b9ffce466f6ed68c8249e8e3926de1811ee97 Mon Sep 17 00:00:00 2001 From: Sjoerd Talsma Date: Mon, 11 Nov 2024 16:31:24 +0100 Subject: [PATCH] Add unit test for OpenTelemetryContextManager Signed-off-by: Sjoerd Talsma --- .../context-manager-opentelemetry/pom.xml | 6 ++ .../OpenTelemetryContextManagerTest.java | 70 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 managers/context-manager-opentelemetry/src/test/java/nl/talsmasoftware/context/managers/opentelemetry/OpenTelemetryContextManagerTest.java diff --git a/managers/context-manager-opentelemetry/pom.xml b/managers/context-manager-opentelemetry/pom.xml index 7c92858a..135561d7 100644 --- a/managers/context-manager-opentelemetry/pom.xml +++ b/managers/context-manager-opentelemetry/pom.xml @@ -50,5 +50,11 @@ io.opentelemetry opentelemetry-context + + + io.opentelemetry + opentelemetry-sdk-testing + test + diff --git a/managers/context-manager-opentelemetry/src/test/java/nl/talsmasoftware/context/managers/opentelemetry/OpenTelemetryContextManagerTest.java b/managers/context-manager-opentelemetry/src/test/java/nl/talsmasoftware/context/managers/opentelemetry/OpenTelemetryContextManagerTest.java new file mode 100644 index 00000000..38d49ead --- /dev/null +++ b/managers/context-manager-opentelemetry/src/test/java/nl/talsmasoftware/context/managers/opentelemetry/OpenTelemetryContextManagerTest.java @@ -0,0 +1,70 @@ +package nl.talsmasoftware.context.managers.opentelemetry; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.ContextKey; +import io.opentelemetry.context.Scope; +import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +class OpenTelemetryContextManagerTest { + @RegisterExtension + static final OpenTelemetryExtension TELEMETRY = OpenTelemetryExtension.create(); + + static OpenTelemetryContextManager subject = OpenTelemetryContextManager.provider(); + + @Test + void getActiveContextValue_delegatesToContext_current() { + // prepare + ContextKey dummyKey = ContextKey.named("dummy"); + String dummyValue = UUID.randomUUID().toString(); + + try (Scope scope = Context.current().with(dummyKey, dummyValue).makeCurrent()) { + // execute + Context activeCtx = subject.getActiveContextValue(); + + // verify + assertThat(activeCtx.get(dummyKey)).isEqualTo(dummyValue); + } + } + + @Test + void initializeNewContext_makes_specified_Context_current() { + // prepare + ContextKey dummyKey = ContextKey.named("dummy"); + String dummyValue = UUID.randomUUID().toString(); + Context newContext = Context.current().with(dummyKey, dummyValue); + assertThat(Context.current().get(dummyKey)).isNull(); + + // execute + try (nl.talsmasoftware.context.api.Context context = subject.initializeNewContext(newContext)) { + + // verify + assertThat(Context.current().get(dummyKey)).isEqualTo(dummyValue); + assertThat(context).isNotNull(); + assertThat(context.getValue()).isSameAs(newContext); + } + + assertThat(Context.current().get(dummyKey)).isNull(); + } + + @Test + void clear_does_nothing_but_also_does_not_throw_exception() { + // prepare + ContextKey dummyKey = ContextKey.named("dummy"); + String dummyValue = UUID.randomUUID().toString(); + try (Scope scope = Context.current().with(dummyKey, dummyValue).makeCurrent()) { + + // execute + assertDoesNotThrow(subject::clear); + + // verify + assertThat(Context.current().get(dummyKey)).isEqualTo(dummyValue); + } + } +}