summaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@oath.com>2018-04-16 16:04:36 +0200
committerBjørn Christian Seime <bjorncs@oath.com>2018-04-16 16:12:11 +0200
commit80bf8ce667fe63af8c6034aa50a1a72b3c51ea02 (patch)
tree584761c6d9ef58d4a2ee729f6dd4a4ce1f935e87 /jdisc_core/src/test
parent7c90afed7e18ebac57ba0dd7986c1a4159f78c98 (diff)
Introduce simple container watchdog
This watchdog uses information from the reference count framework in jdisc_core. Contains no use of System.gc() or WeakReference.
Diffstat (limited to 'jdisc_core/src/test')
-rw-r--r--jdisc_core/src/test/java/com/yahoo/jdisc/core/ContainerWatchdogTest.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/core/ContainerWatchdogTest.java b/jdisc_core/src/test/java/com/yahoo/jdisc/core/ContainerWatchdogTest.java
new file mode 100644
index 00000000000..94e7c3a1d22
--- /dev/null
+++ b/jdisc_core/src/test/java/com/yahoo/jdisc/core/ContainerWatchdogTest.java
@@ -0,0 +1,73 @@
+// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.jdisc.core;
+
+import com.yahoo.jdisc.Metric;
+import com.yahoo.jdisc.test.TestDriver;
+import com.yahoo.test.ManualClock;
+import org.junit.Test;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+/**
+ * @author bjorncs
+ */
+public class ContainerWatchdogTest {
+
+ @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));
+ }
+
+ private static int runMonitorStepAndGetStaleContainerCount(ContainerWatchdog watchdog, DummyMetric metric) {
+ watchdog.monitorDeactivatedContainers();
+ watchdog.emitMetrics(metric);
+ return metric.value;
+ }
+
+ private static class DummyMetric implements Metric {
+ int value;
+
+ @Override
+ public void set(String key, Number val, Context ctx) {
+ this.value = val.intValue();
+ }
+
+ @Override
+ public void add(String key, Number val, Context ctx) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Context createContext(Map<String, ?> properties) {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
+