azkaban-aplcache

Improve InteractiveTestJob (#1928) - Avoid busy while loop

9/13/2018 8:23:24 PM

Details

diff --git a/azkaban-common/src/test/java/azkaban/executor/InteractiveTestJob.java b/azkaban-common/src/test/java/azkaban/executor/InteractiveTestJob.java
index 8f85c7b..e160623 100644
--- a/azkaban-common/src/test/java/azkaban/executor/InteractiveTestJob.java
+++ b/azkaban-common/src/test/java/azkaban/executor/InteractiveTestJob.java
@@ -112,29 +112,37 @@ public class InteractiveTestJob extends AbstractProcessJob {
       throw new RuntimeException("Forced failure of " + getId());
     }
 
+    boolean succeedAfterSleep = this.jobProps.containsKey("fail");
+
+    final long waitMillis;
+    if (succeedAfterSleep) {
+      waitMillis = this.jobProps.getInt("seconds", 10) * 1000L;
+    } else {
+      // this means that job should not exit without external interaction, so exact wait time
+      // doesn't matter. have some non-zero value to avoid busy-looping.
+      waitMillis = 10_000L;
+    }
+
     while (this.isWaiting) {
       synchronized (this) {
-        final int waitMillis = this.jobProps.getInt("seconds", 10) * 1000;
         if (waitMillis > 0) {
           try {
             wait(waitMillis);
           } catch (final InterruptedException e) {
           }
         }
-        if (this.jobProps.containsKey("fail")) {
+        if (succeedAfterSleep) {
           generateProperties(propFiles[1]);
           succeedJob();
         }
-
-        if (!this.isWaiting) {
-          if (!this.succeed) {
-            throw new RuntimeException("Forced failure of " + getId());
-          } else {
-            info("Job " + getId() + " succeeded.");
-          }
-        }
       }
     }
+
+    if (!this.succeed) {
+      throw new RuntimeException("Forced failure of " + getId());
+    } else {
+      info("Job " + getId() + " succeeded.");
+    }
   }
 
   public void failJob() {