Class StrictContextStorage

  • All Implemented Interfaces:
    ContextStorage

    public final class StrictContextStorage
    extends Object
    implements ContextStorage
    A ContextStorage which keeps track of opened and closed Scopes, reporting caller information if a Scope 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 Detail

      • 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.