Deconstructor implements ComponentDeconstructor { @Override public void deconstruct(Collection<Object> components, Collection<Bundle> bundles) { Collection<AbstractComponent> destructibleComponents = new ArrayList<>(); for (var component : components) { if (component instanceof AbstractComponent) { AbstractComponent abstractComponent = (AbstractComponent) component; if (abstractComponent.isDeconstructable()) { destructibleComponents.add(abstractComponent); } } else if (component instanceof Provider) { log.log(FINE, () -> "Starting deconstruction of provider " + component); ((Provider<?>) component).deconstruct(); log.log(FINE, () -> "Finished deconstruction of provider " + component); } else if (component instanceof SharedResource) { log.log(FINE, () -> "Releasing container reference to resource " + component); ((SharedResource) component).release(); } } if (! destructibleComponents.isEmpty() || ! bundles.isEmpty()) executor.schedule(new DestructComponentTask(destructibleComponents, bundles), delay.getSeconds(), TimeUnit.SECONDS); } Deconstructor(boolean delayDeconstruction); @Override void deconstruct(Collection<Object> components, Collection<Bundle> bundles);  }
@Test public void require_abstract_component_destructed() throws InterruptedException { TestAbstractComponent abstractComponent = new TestAbstractComponent(); deconstructor.deconstruct(singleton(abstractComponent), emptyList()); int cnt = 0; while (! abstractComponent.destructed && (cnt++ < 12000)) { Thread.sleep(10); } assertTrue(abstractComponent.destructed); }
@Test public void require_provider_destructed() { TestProvider provider = new TestProvider(); deconstructor.deconstruct(singleton(provider), emptyList()); assertTrue(provider.destructed); }
@Test public void require_shared_resource_released() { TestSharedResource sharedResource = new TestSharedResource(); deconstructor.deconstruct(singleton(sharedResource), emptyList()); assertTrue(sharedResource.released); }
@Test public void bundles_are_uninstalled() throws InterruptedException { var bundle = new UninstallableMockBundle(); deconstructor.deconstruct(emptyList(), singleton(bundle)); int cnt = 0; while (! bundle.uninstalled && (cnt++ < 12000)) { Thread.sleep(10); } assertTrue(bundle.uninstalled); }