Package io.opentelemetry.context
Class StrictContextStorage
- java.lang.Object
-
- io.opentelemetry.context.StrictContextStorage
-
- All Implemented Interfaces:
ContextStorage
public final class StrictContextStorage extends Object implements ContextStorage
AContextStorage
which keeps track of opened and closedScope
s, reporting caller information if aScope
is closed incorrectly or not at all. This is useful in instrumentation tests to check whether any scopes leaked.> class MyInstrumentationTest { > private static ContextStorage previousStorage; > private static StrictContextStorage strictStorage; > > @BeforeAll > static void setUp() { > previousStorage = ContextStorage.get() > strictStorage = StrictContextStorage.create(previousStorage); > ContextStorage.set(strictStorage); > } > > @AfterEach > void checkScopes() { > strictStorage.ensureAllClosed(); > } > > @AfterAll > static void tearDown() { > ContextStorage.set(previousStorage); > } > > @Test > void badTest() { > Context.root().makeCurrent(); > } > }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Scope
attach(Context context)
static StrictContextStorage
create(ContextStorage delegate)
Returns a newStrictContextStorage
which delegates to the providedContextStorage
, wrapping created scopes to track their usage.Context
current()
Returns the currentContext
.void
ensureAllClosed()
Ensures all scopes that have been created by this storage have been closed.
-
-
-
Method Detail
-
create
public static StrictContextStorage create(ContextStorage delegate)
Returns a newStrictContextStorage
which delegates to the providedContextStorage
, wrapping created scopes to track their usage.
-
attach
public Scope attach(Context context)
Description copied from interface:ContextStorage
Sets the specifiedContext
as the currentContext
and returns aScope
representing the scope of execution.Scope.close()
must be called when the currentContext
should be restored to what it was before attachingtoAttach
.- Specified by:
attach
in interfaceContextStorage
-
current
public Context current()
Description copied from interface:ContextStorage
- Specified by:
current
in interfaceContextStorage
-
ensureAllClosed
public void ensureAllClosed()
Ensures all scopes that have been created by this storage have been closed. This can be useful to call at the end of a test to make sure everything has been cleaned up.Note: It is important to close all resources prior to calling this, so that in-flight operations are not mistaken as scope leaks. If this raises an error, consider if a Context.wrap(Executor) wrapped executor} is still running.
- Throws:
AssertionError
- if any scopes were left unclosed.
-
-