changeset 279:4ef47fad072a

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
author Simon Tooke <stooke@redhat.com>
date Tue, 17 Oct 2017 08:38:18 -0400
parents 0e78f20b300f
children 262f254b0ac0
files tests/test-utils/src/main/java/com/redhat/thermostat/gateway/tests/utils/MongodTestUtil.java
diffstat 1 files changed, 35 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- 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<Path>() {
-            @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<Path>() {
+                    @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 {