Skip to content

Commit

Permalink
fix assisted inject predestroy (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcellucci authored May 1, 2019
1 parent c1f4bb1 commit 6f30d10
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.netflix.governator.internal;

import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.inject.Binding;
import com.google.inject.Key;
Expand Down Expand Up @@ -31,7 +30,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
Expand Down Expand Up @@ -257,6 +255,8 @@ public Boolean visitScope(Scope scope) {
if (scope.equals(Scopes.SINGLETON)
|| (scope instanceof AbstractScope && ((AbstractScope) scope).isSingletonScope())) {
scopedMarkerProvider = Providers.of(scopeCleaner.singletonMarker);
} else if (scope.equals(Scopes.NO_SCOPE)) {
return visitNoScoping();
} else {
scopedMarkerProvider = scope.scope(ScopeCleanupMarker.MARKER_KEY, scopeCleaner);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.netflix.governator;

import java.util.concurrent.atomic.AtomicBoolean;

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

import com.google.inject.Module;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.netflix.governator.InjectorBuilder;
import com.netflix.governator.LifecycleInjector;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AssistedInjectPreDestroyTest {
private final Logger log = LoggerFactory.getLogger(getClass());

@Test
public void testAssistedInject() throws Exception {
Module assistedInjectModule = new FactoryModuleBuilder()
.implement(AnInterestingClient.class, InterestingClientImpl.class)
.build(InterestingClientFactory.class);

final AnInterestingClient interestingClient;
try (LifecycleInjector injector = InjectorBuilder.fromModule(assistedInjectModule).createInjector()) {
interestingClient = injector.getInstance(InterestingClientFactory.class).create("something");

log.info("Init is all done, I'll pretend to be doing something for a bit");
Thread.sleep(1_000L);
Assert.assertFalse(interestingClient.isClosed());

log.info("Kicking the GC to see if it triggers unintended cleanups");
System.gc();
Thread.sleep(1_000L);
Assert.assertFalse(interestingClient.isClosed());

log.info("Just woke up again, about to close shop");
interestingClient.setOkToClose();
}
// injector is closed by try block exit above, which triggers PreDestroy /
// AutoCloseable methods
Assert.assertTrue(interestingClient.isClosed());
log.info("Done closing shop, bye");
}
}

interface AnInterestingClient {
void setOkToClose();

boolean isClosed();
}

class InterestingClientImpl implements AnInterestingClient {
private static final Logger logger = LoggerFactory.getLogger(InterestingClientImpl.class);
private final AtomicBoolean isOkToClose = new AtomicBoolean(false);
private final AtomicBoolean closedBeforeExpected = new AtomicBoolean(false);
private final AtomicBoolean closed = new AtomicBoolean(false);

@Inject
InterestingClientImpl(@Assisted String aParameter) {
}

@Override
public void setOkToClose() {
if (closedBeforeExpected.get()) {
throw new IllegalStateException("Someone called close before we were ready!");
}
isOkToClose.set(true);
}

@Override
public boolean isClosed() {
return closed.get();
}

@PreDestroy
public void close() {
closed.set(true);
if (!isOkToClose.get()) {
closedBeforeExpected.set(true);
logger.info("Someone called close() on me ({}) and I'm mad about it", this);

} else {
logger.info("Someone called close() on me ({}) and I'm ok with that", this);
}
}
}

interface InterestingClientFactory {
AnInterestingClient create(@Assisted String aParameter);
}

0 comments on commit 6f30d10

Please sign in to comment.