JAL-3599 Tidyup for all OS to use ScheduledExecutor
authorBen Soares <b.soares@dundee.ac.uk>
Tue, 13 Jun 2023 11:16:10 +0000 (12:16 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Tue, 13 Jun 2023 11:16:10 +0000 (12:16 +0100)
build.gradle

index 1c73079..a47a7fd 100644 (file)
@@ -4257,67 +4257,87 @@ task jalviewjsLaunchTest {
   def stderr
   doFirst {
     def timeoutms = Integer.valueOf(jalviewjs_chromium_timeout) * 1000
-
-    ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
-    final Future handler = executor.submit(
-      () -> {
-
-        exec {
-          def binary = file(chromiumBinary)
-          if (!binary.exists()) {
-            throw new StopExecutionException("Could not find chromium binary '${chromiumBinary}'. Cannot run task ${name}.")
+    
+    def binary = file(chromiumBinary)
+    if (!binary.exists()) {
+      throw new StopExecutionException("Could not find chromium binary '${chromiumBinary}'. Cannot run task ${name}.")
+    }
+    stdout = new ByteArrayOutputStream()
+    stderr = new ByteArrayOutputStream()
+    def execStdout
+    def execStderr
+    if (jalviewjs_j2s_to_console.equals("true")) {
+      execStdout = new org.apache.tools.ant.util.TeeOutputStream(
+        stdout,
+        System.out)
+      execStderr = new org.apache.tools.ant.util.TeeOutputStream(
+        stderr,
+        System.err)
+    } else {
+      execStdout = stdout
+      execStderr = stderr
+    }
+    def execArgs = [
+      "--no-sandbox", // --no-sandbox IS USED BY THE THORIUM APPIMAGE ON THE BUILDSERVER
+      "--headless=new",
+      "--disable-gpu",
+      "--timeout=${timeoutms}",
+      "--virtual-time-budget=${timeoutms}",
+      "--user-data-dir=${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}",
+      "--profile-directory=${jalviewjs_chromium_profile_name}",
+      "--allow-file-access-from-files",
+      "--enable-logging=stderr",
+      "file://${jalviewDirAbsolutePath}/${jalviewjsStderrLaunchFilename}"
+    ]
+    
+    if (true || macOS) {
+      ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
+      Future f1 = executor.submit(
+        () -> {
+          exec {
+            standardOutput = execStdout
+            errorOutput = execStderr
+            executable(chromiumBinary)
+            args(execArgs)
           }
+          executor.shutdownNow()
+        }
+      )
 
-          stdout = new ByteArrayOutputStream()
-          stderr = new ByteArrayOutputStream()
-          if (jalviewjs_j2s_to_console.equals("true")) {
-            standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
-              stdout,
-              System.out)
-            errorOutput = new org.apache.tools.ant.util.TeeOutputStream(
-              stderr,
-              System.err)
+      def noChangeBytes = 0
+      def noChangeIterations = 0
+      executor.scheduleAtFixedRate(
+        () -> {
+          String stderrString = stderr.toString()
+          // shutdown the task if we have a success string
+          if (stderrString.contains(jalviewjs_desktop_init_string)) {
+            f1.cancel()
+            Thread.sleep(1000)
+            executor.shutdownNow()
+          }
+          // if no change in stderr for 10s then also end
+          if (noChangeIterations >= 10) {
+            executor.shutdownNow()
+          }
+          if (stderrString.length() == noChangeBytes) {
+            noChangeIterations++
           } else {
-            standardOutput = stdout
-            errorOutput = stderr
+            noChangeBytes = stderrString.length()
+            noChangeIterations = 0
           }
+        },
+        1, 1, TimeUnit.SECONDS)
 
-
-          executable(chromiumBinary)
-          args([
-            "--no-sandbox", // --no-sandbox IS USED BY THE THORIUM APPIMAGE ON THE BUILDSERVER
-            "--headless=new",
-            "--disable-gpu",
-            "--timeout=${timeoutms}",
-            "--virtual-time-budget=${timeoutms}",
-            "--user-data-dir=${jalviewDirAbsolutePath}/${jalviewjsBuildDir}/${jalviewjs_chromium_user_dir}",
-            "--profile-directory=${jalviewjs_chromium_profile_name}",
-            "--allow-file-access-from-files",
-            "--enable-logging=stderr",
-            "file://${jalviewDirAbsolutePath}/${jalviewjsStderrLaunchFilename}"
-          ])
-        }
-
-      }
-
-    )
-
-    executor.schedule(new Runnable(){
-      public void run(){
-         executor.shutdownNow()
-      }
-    }, timeoutms, TimeUnit.MILLISECONDS);
-
-    executor.scheduleAtFixedRate(
-      () -> {
-        if (stderr.toString().contains(jalviewjs_desktop_init_string)) {
-          Thread.sleep(1000)
+      executor.schedule(new Runnable(){
+        public void run(){
+          f1.cancel()
           executor.shutdownNow()
         }
-      },
-      1, 1, TimeUnit.SECONDS)
+      }, timeoutms, TimeUnit.MILLISECONDS)
 
-    executor.awaitTermination(timeoutms, TimeUnit.MILLISECONDS)
+      executor.awaitTermination(timeoutms+10000, TimeUnit.MILLISECONDS)
+      executor.shutdownNow()
+    }
 
   }