Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix out-of-order shutdown for eager singletons #355

Merged
merged 1 commit into from Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -206,13 +206,12 @@ private ManagedInstanceScopingVisitor(Object injectee, Object context,
}

/*
* add a hard-reference ManagedInstanceAction to cleanupActions. Cleanup is triggered only at injector shutdown.
* handle eager singletons same as singletons for cleanup purposes.
*
*/
@Override
public Boolean visitEagerSingleton() {
cleanupActions.addFirst(new ManagedInstanceAction(injectee, lifecycleActions));
return true;
public Boolean visitEagerSingleton() {
return visitScope(Scopes.SINGLETON);
}

/*
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.junit.Assert;
Expand Down Expand Up @@ -172,6 +173,56 @@ public void shutdown2() {
}


private static class EagerBean {
volatile boolean shutdown = false;
SingletonBean singletonInstance;
@Inject
public EagerBean(SingletonBean singletonInstance) {
this.singletonInstance = singletonInstance;
}

@PreDestroy
public void shutdown() {
System.out.println("eager bean shutdown invoked");
shutdown = true;
this.singletonInstance.eagerShutdown = true;
}
}

@Singleton
private static class SingletonBean {
volatile boolean eagerShutdown = false;
boolean shutdown = false;

@PreDestroy
public void shutdown() {
System.out.println("singleton bean shutdown invoked");
shutdown = true;
Assert.assertTrue(eagerShutdown);
}
}


@Test
public void testEagerSingletonShutdown() {
EagerBean eagerBean;
SingletonBean singletonBean;
try (LifecycleInjector injector = InjectorBuilder.fromModule(new AbstractModule() {
@Override
protected void configure() {
bind(EagerBean.class).asEagerSingleton();
bind(SingletonBean.class).in(Scopes.SINGLETON);
}}).createInjector()) {
eagerBean = injector.getInstance(EagerBean.class);
singletonBean = injector.getInstance(SingletonBean.class);
Assert.assertFalse(eagerBean.shutdown);
Assert.assertFalse(singletonBean.shutdown);
}
Assert.assertTrue(eagerBean.shutdown);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the test confirm the expected shutdown ordering as well?

Assert.assertTrue(singletonBean.shutdown);
}


@Test
public void testLifecycleShutdownInheritance1() {
final PreDestroyChild1 preDestroyChild = Mockito.spy(new PreDestroyChild1());
Expand Down