Rework time keeping 34/111534/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 25 Apr 2024 20:36:09 +0000 (22:36 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 25 Apr 2024 20:40:48 +0000 (22:40 +0200)
Do not rely on calendar time and use more modern methods of sleeping.

JIRA: ODLPARENT-312
Change-Id: Id7c3bb323e0b49f511bd58d5ae71d87feeabe8c1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
features-test-plugin/src/main/java/org/opendaylight/odlparent/features/test/plugin/TestProbe.java

index 42c0b573bac0f476dbc7f68c6b50be1312d8283b..27905ded3638c256cf09759e98d1bbd0b9a82d68 100644 (file)
@@ -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;
         }
     }