diff options
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; } + } } |