# HG changeset patch # User Simon Tooke # Date 1508243898 14400 # Node ID 4ef47fad072ab0636e85167e7c17fc2327ac6cc0 # Parent 0e78f20b300f4abd191f0c1524621b113c280d2a Retry test cleanup This small patch retries the test cleanup code 5 times until it succeeds or it gives up - on windows the jetty temp files now get deleted properly. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025404.html diff -r 0e78f20b300f -r 4ef47fad072a tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java --- a/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java Tue Oct 10 11:58:42 2017 +0200 +++ b/tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java Tue Oct 17 08:38:18 2017 -0400 @@ -126,20 +126,43 @@ } private void finish() throws IOException { - Files.walkFileTree(tempDbDir, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } + + // some environments (Windows) may still have the file marked as in use for a few seconds + // this code retries a few times, then gives up. + + final int NUM_RETRIES = 5; + final int WAIT_TIME_MILLIS = 1000; + IOException savedException = null; + for (int i = NUM_RETRIES; i > 0; i--) { + try { + Files.walkFileTree(tempDbDir, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + + }); + savedException = null; + break; + } catch (IOException fsException) { + savedException = fsException; + try { + Thread.sleep(WAIT_TIME_MILLIS); + } catch (InterruptedException ignored) { + // we don't really care if we got interrupted as we'll try this a few times + } } - - }); + } + if (savedException != null) { + throw savedException; + } } private void waitForSocketToBind(String host, int port) throws InterruptedException {