-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for OpenTelemetryContextManager
Signed-off-by: Sjoerd Talsma <[email protected]>
- Loading branch information
1 parent
59c0501
commit 851b9ff
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ava/nl/talsmasoftware/context/managers/opentelemetry/OpenTelemetryContextManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Object> 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<Object> 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> 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<Object> 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); | ||
} | ||
} | ||
} |