summaryrefslogtreecommitdiffstats
path: root/container-core
diff options
context:
space:
mode:
Diffstat (limited to 'container-core')
-rw-r--r--container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java13
-rw-r--r--container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java30
2 files changed, 22 insertions, 21 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java
index 6ccd25ad6c7..a5be5cb0b0a 100644
--- a/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java
+++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/StateMonitor.java
@@ -11,6 +11,7 @@ import com.yahoo.log.LogLevel;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
@@ -38,12 +39,19 @@ public class StateMonitor extends AbstractComponent {
@Inject
public StateMonitor(HealthMonitorConfig config, Timer timer) {
+ this(config, timer, runnable -> {
+ Thread thread = new Thread(runnable, "StateMonitor");
+ thread.setDaemon(true);
+ return thread;
+ });
+ }
+
+ StateMonitor(HealthMonitorConfig config, Timer timer, ThreadFactory threadFactory) {
this.timer = timer;
this.snapshotIntervalMs = (long)(config.snapshot_interval() * TimeUnit.SECONDS.toMillis(1));
this.lastSnapshotTimeMs = timer.currentTimeMillis();
this.status = Status.valueOf(config.initialStatus());
- thread = new Thread(StateMonitor.this::run, "StateMonitor");
- thread.setDaemon(true);
+ thread = threadFactory.newThread(this::run);
thread.start();
}
@@ -69,6 +77,7 @@ public class StateMonitor extends AbstractComponent {
/** Returns the interval between each metrics snapshot used by this */
public long getSnapshotIntervalMillis() { return snapshotIntervalMs; }
+ /** NOTE: For unit testing only. May lead to undefined behaviour if StateMonitor thread is running simultaneously **/
boolean checkTime() {
long now = timer.currentTimeMillis();
if (now < lastSnapshotTimeMs + snapshotIntervalMs) {
diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java b/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java
index 5d8f885f8d0..a4c993f8c39 100644
--- a/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java
+++ b/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java
@@ -29,6 +29,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
+import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@@ -36,6 +37,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
/**
* @author Simon Thoresen Hult
@@ -51,30 +53,20 @@ public class StateHandlerTest {
@Before
public void startTestDriver() {
- driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(new AbstractModule() {
-
+ Timer timer = this.currentTimeMillis::get;
+ this.driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(new AbstractModule() {
@Override
protected void configure() {
- bind(Timer.class).toInstance(new Timer() {
-
- @Override
- public long currentTimeMillis() {
- return currentTimeMillis.get();
- }
- });
+ bind(Timer.class).toInstance(timer);
}
});
ContainerBuilder builder = driver.newContainerBuilder();
- builder.guiceModules().install(new AbstractModule() {
-
- @Override
- protected void configure() {
- bind(HealthMonitorConfig.class)
- .toInstance(new HealthMonitorConfig(new HealthMonitorConfig.Builder().snapshot_interval(
- TimeUnit.MILLISECONDS.toSeconds(SNAPSHOT_INTERVAL))));
- }
- });
- monitor = builder.guiceModules().getInstance(StateMonitor.class);
+ HealthMonitorConfig healthMonitorConfig =
+ new HealthMonitorConfig(
+ new HealthMonitorConfig.Builder()
+ .snapshot_interval(TimeUnit.MILLISECONDS.toSeconds(SNAPSHOT_INTERVAL)));
+ ThreadFactory threadFactory = ignored -> mock(Thread.class);
+ this.monitor = new StateMonitor(healthMonitorConfig, timer, threadFactory);
builder.guiceModules().install(new AbstractModule() {
@Override