From 59f02f2cad73aff2d586b440d92a889835b0eb3f Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Tue, 24 Nov 2020 09:19:52 +0100 Subject: Avoid Thread.sleep(1000) in unit test --- .../com/yahoo/messagebus/ConfigAgentTestCase.java | 36 +++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'messagebus/src/test/java/com/yahoo') diff --git a/messagebus/src/test/java/com/yahoo/messagebus/ConfigAgentTestCase.java b/messagebus/src/test/java/com/yahoo/messagebus/ConfigAgentTestCase.java index 3fbc5d25cd9..02fe407b3de 100755 --- a/messagebus/src/test/java/com/yahoo/messagebus/ConfigAgentTestCase.java +++ b/messagebus/src/test/java/com/yahoo/messagebus/ConfigAgentTestCase.java @@ -3,21 +3,16 @@ package com.yahoo.messagebus; import com.yahoo.config.subscription.ConfigSet; import com.yahoo.config.subscription.ConfigURI; -import com.yahoo.messagebus.routing.HopSpec; -import com.yahoo.messagebus.routing.RouteSpec; import com.yahoo.messagebus.routing.RoutingSpec; -import com.yahoo.messagebus.routing.RoutingTableSpec; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Simon Thoresen Hult @@ -28,7 +23,7 @@ public class ConfigAgentTestCase { public TemporaryFolder tmpFolder = new TemporaryFolder(); @Test - public void testRoutingConfig() throws InterruptedException, IOException { + public void testRoutingConfig() throws InterruptedException { LocalHandler handler = new LocalHandler(); assertFalse(testHalf(handler.spec)); assertFalse(testFull(handler.spec)); @@ -174,26 +169,23 @@ public class ConfigAgentTestCase { private static class LocalHandler implements ConfigHandler { - volatile RoutingSpec spec = new RoutingSpec(); + RoutingSpec spec = new RoutingSpec(); - public void setupRouting(RoutingSpec spec) { + public synchronized void setupRouting(RoutingSpec spec) { this.spec = spec; + notify(); } - public void reset() { + public synchronized void reset() { spec = null; } - public boolean await(int timeout, TimeUnit unit) throws InterruptedException { - long millis = System.currentTimeMillis() + unit.toMillis(timeout); - while (spec == null) { - long now = System.currentTimeMillis(); - if (now >= millis) { - return false; - } - Thread.sleep(1000); - } - return true; + public synchronized boolean await(int timeout, TimeUnit unit) throws InterruptedException { + long remaining, doom = System.currentTimeMillis() + unit.toMillis(timeout); + while (spec == null && (remaining = doom - System.currentTimeMillis()) > 0) + wait(remaining); + + return spec != null; } } } -- cgit v1.2.3