From: Robert Varga Date: Thu, 25 Apr 2024 20:36:09 +0000 (+0200) Subject: Rework time keeping X-Git-Tag: v13.1.2~19 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=5a1fe6bdafffddbded87d1cba3d2100b3b948886;p=odlparent.git Rework time keeping Do not rely on calendar time and use more modern methods of sleeping. JIRA: ODLPARENT-312 Change-Id: Id7c3bb323e0b49f511bd58d5ae71d87feeabe8c1 Signed-off-by: Robert Varga --- diff --git a/features-test-plugin/src/main/java/org/opendaylight/odlparent/features/test/plugin/TestProbe.java b/features-test-plugin/src/main/java/org/opendaylight/odlparent/features/test/plugin/TestProbe.java index 42c0b573b..27905ded3 100644 --- a/features-test-plugin/src/main/java/org/opendaylight/odlparent/features/test/plugin/TestProbe.java +++ b/features-test-plugin/src/main/java/org/opendaylight/odlparent/features/test/plugin/TestProbe.java @@ -16,6 +16,7 @@ import java.util.Arrays; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.inject.Inject; import org.apache.karaf.bundle.core.BundleService; @@ -140,24 +141,28 @@ public final class TestProbe { final int interval = Integer.parseInt(System.getProperty(BUNDLE_CHECK_INTERVAL_SECONDS, DEFAULT_INTERVAL)); LOG.info("Checking bundle states. Interval = {} second(s). Timeout = {} second(s).", interval, timeout); - final var maxTimestamp = System.currentTimeMillis() + timeout * 1000L; - CheckResult result = CheckResult.IN_PROGRESS; - while (System.currentTimeMillis() < maxTimestamp) { + final var maxNanos = TimeUnit.SECONDS.toNanos(timeout); + final var started = System.nanoTime(); + while (true) { Arrays.stream(bundleContext.getBundles()).forEach(this::captureBundleState); - result = aggregatedCheckResults(); - if (result != CheckResult.IN_PROGRESS) { - break; + final var result = aggregatedCheckResults(); + if (result == CheckResult.IN_PROGRESS) { + final var now = System.nanoTime(); + if (now - started >= maxNanos) { + logNokBundleDetails(); + throw new IllegalStateException("Bundles states check timeout"); + } + + TimeUnit.SECONDS.sleep(interval); + continue; } - Thread.sleep(interval * 1000L); - } - LOG.info("Bundle state check completed with result {}", result); - if (result == CheckResult.IN_PROGRESS) { - logNokBundleDetails(); - throw new IllegalStateException("Bundles states check timeout"); - } - if (result != CheckResult.SUCCESS) { - logNokBundleDetails(); - throw new IllegalStateException("Bundle states check failure"); + + LOG.info("Bundle state check completed with result {}", result); + if (result != CheckResult.SUCCESS) { + logNokBundleDetails(); + throw new IllegalStateException("Bundle states check failure"); + } + break; } }