From 05d9135d2b9f574fcd0a7ae8f863a2b4710bb60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Mon, 28 Oct 2024 14:28:24 +0100 Subject: [PATCH] [test] AwtScreenshot: better error handling * On error exit with exit code 3 * add std.err to error message https://github.com/eclipse-platform/eclipse.platform.swt/issues/1518 --- .../src/org/eclipse/test/AwtScreenshot.java | 57 ++++++++----------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/eclipse.platform.releng/bundles/org.eclipse.test/src/org/eclipse/test/AwtScreenshot.java b/eclipse.platform.releng/bundles/org.eclipse.test/src/org/eclipse/test/AwtScreenshot.java index cdea5023f82..0601cf1763e 100644 --- a/eclipse.platform.releng/bundles/org.eclipse.test/src/org/eclipse/test/AwtScreenshot.java +++ b/eclipse.platform.releng/bundles/org.eclipse.test/src/org/eclipse/test/AwtScreenshot.java @@ -21,6 +21,7 @@ import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -28,6 +29,7 @@ import java.io.PrintStream; import java.net.URISyntaxException; import java.net.URL; +import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; @@ -47,6 +49,7 @@ public static void main(String[] args) { System.out.println("AWT screenshot saved to: " + file.getAbsolutePath()); } catch (HeadlessException | AWTException | IOException e) { e.printStackTrace(); + System.exit(3); } } @@ -88,49 +91,39 @@ && new File(cp + "bin" + File.separatorChar).isDirectory()) { javaExe += ".exe"; // assume it's Windows } String[] args = new String[] { javaExe, "-cp", cp, AwtScreenshot.class.getName(), screenshotFile }; - // System.out.println("Start process: " + Arrays.asList(args)); ProcessBuilder processBuilder = new ProcessBuilder(args); if ("Mac OS X".equals(System.getProperty("os.name"))) { processBuilder.environment().put("AWT_TOOLKIT", "CToolkit"); } Process process = processBuilder.start(); - - @SuppressWarnings("resource") // never close process streams - InputStream errorStream = process.getErrorStream(); - - @SuppressWarnings("resource") // never close process streams - InputStream inputStream = process.getInputStream(); - - new StreamForwarder(errorStream, System.out).start(); - new StreamForwarder(inputStream, System.out).start(); - long end = System.currentTimeMillis() + TIMEOUT_SECONDS * 1000; - boolean done = false; - do { + try (InputStream errorStream = process.getErrorStream(); + InputStream inputStream = process.getInputStream()) { + ByteArrayOutputStream errorOut = new ByteArrayOutputStream(); + new StreamForwarder(errorStream, new PrintStream(errorOut)).start(); + new StreamForwarder(inputStream, System.out).start(); try { - process.exitValue(); - done = true; - } catch (IllegalThreadStateException e) { - try { - Thread.sleep(100); - } catch (InterruptedException e1) { - // continue - } + process.waitFor(TIMEOUT_SECONDS, TimeUnit.SECONDS); + } catch (InterruptedException ie) { + ie.printStackTrace(); } - } while (!done && System.currentTimeMillis() < end); - if (done) { - int exitCode = process.exitValue(); - if (exitCode != 0) { - new RuntimeException("AwtScreenshot VM finished with exit code " + exitCode + ".") - .printStackTrace(); + if (!process.isAlive()) { + int exitCode = process.exitValue(); + if (exitCode != 0) { + throw new RuntimeException( + "AwtScreenshot VM finished with exit code " + exitCode + ":\n" + errorOut.toString()); + } + if (errorOut.size() > 0) { + System.out.println(errorOut.toString()); + } + } else { + process.destroy(); + throw new RuntimeException( + "Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds:\n" + errorOut.toString()); } - } else { - process.destroy(); - new RuntimeException("Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds.") - .printStackTrace(); } } catch (URISyntaxException | IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } } }