diff options
author | Valerij Fredriksen <valerijf@verizonmedia.com> | 2020-06-16 08:54:24 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@verizonmedia.com> | 2020-06-16 08:54:24 +0200 |
commit | f64388e0b095e77707cfa444a93e0e9e251fab41 (patch) | |
tree | 66d5655114de80dba33c17a9951c66b3de86adb8 /node-admin | |
parent | f5150dcb06c0b6eedb2aebf0c7558fde18b99647 (diff) |
Simplify with CountDownLatch and Callable
Diffstat (limited to 'node-admin')
-rw-r--r-- | node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java | 38 |
1 files changed, 12 insertions, 26 deletions
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java index 99111366b8e..195b07bd73f 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/nodeagent/NodeAgentContextManagerTest.java @@ -7,6 +7,8 @@ import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -146,46 +148,30 @@ public class NodeAgentContextManagerTest { } private static class AsyncExecutor<T> { - private final Object monitor = new Object(); - private final Thread thread; + private final CountDownLatch latch = new CountDownLatch(1); private volatile Optional<T> response = Optional.empty(); private volatile Optional<Exception> exception = Optional.empty(); - private boolean completed = false; - private AsyncExecutor(ThrowingSupplier<T> supplier) { - this.thread = new Thread(() -> { + private AsyncExecutor(Callable<T> supplier) { + new Thread(() -> { try { - response = Optional.of(supplier.get()); + response = Optional.of(supplier.call()); } catch (Exception e) { exception = Optional.of(e); } - synchronized (monitor) { - completed = true; - monitor.notifyAll(); - } - }); - this.thread.start(); + latch.countDown(); + }).start(); } private AsyncExecutor<T> awaitResult() { - synchronized (monitor) { - while (!completed) { - try { - monitor.wait(); - } catch (InterruptedException ignored) { } - } - } + try { + latch.await(); + } catch (InterruptedException ignored) { } return this; } private boolean isCompleted() { - synchronized (monitor) { - return completed; - } + return latch.getCount() == 0; } } - - private interface ThrowingSupplier<T> { - T get() throws Exception; - } }
\ No newline at end of file |