From 4698a5c96de864d2af3b381ce9929373a6ea855c Mon Sep 17 00:00:00 2001 From: David Dehaven Date: Sun, 9 Dec 2012 07:43:12 -0800 Subject: [PATCH] 8004042: Arrrghs.java test failed on windows with access error Reviewed-by: smarks, jjh, ksrini --- jdk/test/tools/launcher/Arrrghs.java | 5 ++- jdk/test/tools/launcher/TestHelper.java | 45 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/jdk/test/tools/launcher/Arrrghs.java b/jdk/test/tools/launcher/Arrrghs.java index 364589f5ad0..9bf9f21b465 100644 --- a/jdk/test/tools/launcher/Arrrghs.java +++ b/jdk/test/tools/launcher/Arrrghs.java @@ -27,7 +27,7 @@ * 6894719 6968053 7151434 7146424 * @summary Argument parsing validation. * @compile -XDignore.symbol.file Arrrghs.java - * @run main Arrrghs + * @run main/othervm Arrrghs */ import java.io.BufferedReader; @@ -204,8 +204,7 @@ public class Arrrghs extends TestHelper { // exiting the process prematurely can terminate the stderr. scratchpad.add(javaCmd + " -version " + inArgs); File batFile = new File("atest.bat"); - java.nio.file.Files.deleteIfExists(batFile.toPath()); - createFile(batFile, scratchpad); + createAFile(batFile, scratchpad); TestResult tr = doExec(batFile.getName()); diff --git a/jdk/test/tools/launcher/TestHelper.java b/jdk/test/tools/launcher/TestHelper.java index e630dd5df54..0115f057142 100644 --- a/jdk/test/tools/launcher/TestHelper.java +++ b/jdk/test/tools/launcher/TestHelper.java @@ -358,6 +358,51 @@ public class TestHelper { Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING); } + /** + * Attempt to create a file at the given location. If an IOException + * occurs then back off for a moment and try again. When a number of + * attempts fail, give up and throw an exception. + */ + void createAFile(File aFile, List contents) throws IOException { + IOException cause = null; + for (int attempts = 0; attempts < 10; attempts++) { + try { + Files.write(aFile.getAbsoluteFile().toPath(), contents, + Charset.defaultCharset(), CREATE, TRUNCATE_EXISTING, WRITE); + if (cause != null) { + /* + * report attempts and errors that were encountered + * for diagnostic purposes + */ + System.err.println("Created batch file " + + aFile + " in " + (attempts + 1) + + " attempts"); + System.err.println("Errors encountered: " + cause); + cause.printStackTrace(); + } + return; + } catch (IOException ioe) { + if (cause != null) { + // chain the exceptions so they all get reported for diagnostics + cause.addSuppressed(ioe); + } else { + cause = ioe; + } + } + + try { + Thread.sleep(500); + } catch (InterruptedException ie) { + if (cause != null) { + // cause should alway be non-null here + ie.addSuppressed(cause); + } + throw new RuntimeException("Interrupted while creating batch file", ie); + } + } + throw new RuntimeException("Unable to create batch file", cause); + } + static void createFile(File outFile, List content) throws IOException { Files.write(outFile.getAbsoluteFile().toPath(), content, Charset.defaultCharset(), CREATE_NEW);