ScannerContext { private void cleanup() { logger.debug("cleanup"); cleanupQueue.forEach(c -> { try { logger.info("invoking cleanup task: {}", c); c.cleanup(this); } catch (Exception e) { logger.warn("problem during cleanup: " + e); } }); }  boolean hasExceptions(); String getName(); Map<String, Object> getMap(); ScannerContext withAttribute(String key, Object val); ScannerContext addCleanupTask(CleanupTask task); ScannerContext withCleanupTask(CleanupTask task); static Optional<ScannerContext> getScannerContext(); long getEntityCount(); long incrementEntityCount(); List<Exception> getExceptions(); ScannerContext markException(Exception e); ScannerContext withName(String name); void exec(Invokable x); String toString();  }
@Test public void testCleanup() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); CleanupTask task = new CleanupTask() { @Override public void cleanup(ScannerContext context) { latch.countDown(); } }; new ScannerContext().withName("test").withCleanupTask(task).exec(ctx -> { Assertions.assertThat(ctx.getMap().get("name")).isEqualTo("test"); }); Assertions.assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue(); }