diff options
author | Bjørn Christian Seime <bjorncs@yahoo-inc.com> | 2017-07-17 14:21:33 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@yahoo-inc.com> | 2017-07-17 14:23:37 +0200 |
commit | ad5a37f5031734bb4bc75148a7d6ad590fdb515a (patch) | |
tree | ae2d727d5482d1632b86dfc3713752af5407f9bf /jdisc_core | |
parent | 01f124796825603023cc46336c68847b8c7ead01 (diff) |
Improve ActiveContainerDeactivationWatchdog unit test
Diffstat (limited to 'jdisc_core')
2 files changed, 17 insertions, 7 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdog.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdog.java index 2438edd101a..588e5293066 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdog.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdog.java @@ -63,6 +63,7 @@ class ActiveContainerDeactivationWatchdog implements ActiveContainerMetrics, Aut ActiveContainerDeactivationWatchdog(Clock clock, ScheduledExecutorService scheduler) { this.clock = clock; this.scheduler = scheduler; + // NOTE: Make sure to update the unit test if the order commands are registered is changed. this.scheduler.scheduleAtFixedRate(this::warnOnStaleContainers, WATCHDOG_FREQUENCY.getSeconds(), WATCHDOG_FREQUENCY.getSeconds(), diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdogTest.java b/jdisc_core/src/test/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdogTest.java index a618b82c5ec..c83458796ed 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdogTest.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/core/ActiveContainerDeactivationWatchdogTest.java @@ -84,12 +84,14 @@ public class ActiveContainerDeactivationWatchdogTest { WeakReference<ActiveContainer> containerWeakReference = new WeakReference<>(container); container = null; // make container instance collectable by GC - System.gc(); - System.runFinalization(); // this is required to trigger enqueuing of phantom references on some Linux systems + + executor.triggerGcCommand.run(); assertNull("Container is not GCed - probably because the watchdog has a concrete reference to it", containerWeakReference.get()); - executor.containerDestructorCommand.run(); + + executor.enforceDestructionOfGarbageCollectedContainersCommand.run(); + assertTrue("Destructor is not called on deactivated container", destructed.get()); } @@ -124,7 +126,9 @@ public class ActiveContainerDeactivationWatchdogTest { private static class ExecutorMock extends ScheduledThreadPoolExecutor { - public Runnable containerDestructorCommand; + public Runnable warnOnStaleContainersCommand; + public Runnable triggerGcCommand; + public Runnable enforceDestructionOfGarbageCollectedContainersCommand; private int registrationCounter = 0; public ExecutorMock() { @@ -133,14 +137,19 @@ public class ActiveContainerDeactivationWatchdogTest { @Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { - if (registrationCounter == 2) { - containerDestructorCommand = command; - } else if (registrationCounter > 2){ + if (registrationCounter == 0) { + warnOnStaleContainersCommand = command; + } else if (registrationCounter == 1) { + triggerGcCommand = command; + } else if (registrationCounter == 2) { + enforceDestructionOfGarbageCollectedContainersCommand = command; + } else { throw new IllegalStateException("Unexpected registration"); } ++registrationCounter; return null; } + } } |