ContainerWatchdog implements ContainerWatchdogMetrics, AutoCloseable { void onContainerActivation(ActiveContainer nextContainer) { synchronized (monitor) { if (currentContainer != null) { deactivatedContainers.add( new DeactivatedContainer(currentContainer, currentContainerActivationTime, clock.instant())); } currentContainer = nextContainer; currentContainerActivationTime = clock.instant(); } } ContainerWatchdog();   ContainerWatchdog(ScheduledExecutorService scheduler, Clock clock); @Override void emitMetrics(Metric metric); @Override void close();  }
@Test public void watchdog_counts_stale_container() { TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(); ManualClock clock = new ManualClock(Instant.EPOCH); DummyMetric metric = new DummyMetric(); ContainerWatchdog watchdog = new ContainerWatchdog(mock(ScheduledExecutorService.class), clock); ActiveContainer containerWithoutRetainedResources = new ActiveContainer(driver.newContainerBuilder()); watchdog.onContainerActivation(containerWithoutRetainedResources); assertEquals(0, runMonitorStepAndGetStaleContainerCount(watchdog, metric)); clock.advance(Duration.ofHours(1)); watchdog.onContainerActivation(null); assertEquals(0, runMonitorStepAndGetStaleContainerCount(watchdog, metric)); clock.advance(ContainerWatchdog.GRACE_PERIOD); assertEquals(0, runMonitorStepAndGetStaleContainerCount(watchdog, metric)); clock.advance(Duration.ofSeconds(1)); assertEquals(1, runMonitorStepAndGetStaleContainerCount(watchdog, metric)); containerWithoutRetainedResources.release(); assertEquals(0, runMonitorStepAndGetStaleContainerCount(watchdog, metric)); }