Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] AwtScreenshot: better error handling #2486

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
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;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}