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

Tests: Add retries to NbTestCase#deleteFile to stabilize unittests #8187

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 35 additions & 11 deletions harness/nbjunit/src/org/netbeans/junit/NbTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -995,18 +996,41 @@ public File getWorkDir() throws IOException {

// private method for deleting a file/directory (and all its subdirectories/files)
private static void deleteFile(File file) throws IOException {
Files.walkFileTree(file.toPath(), new SimpleFileVisitor<java.nio.file.Path>() {
@Override
public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
final int MAX_TRIES = 25; // => 5s retries
for (int i = 0; i < MAX_TRIES; i++) {
try {
Files.walkFileTree(file.toPath(), new SimpleFileVisitor<java.nio.file.Path>() {
@Override
public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
break;
} catch (IOException ex) {
// The recursive deletion can fail with an IOException, because
// new files are created, while the directory tree is traversed
// Observed where `archives.properties` and `segments` from
// parsing API. Both are saved delayed.
// Swallow the exception and retry. If last try is reached,
// throw.
if ((i + 1) == MAX_TRIES) {
throw ex;
} else {
try {
Thread.sleep(200);
} catch (InterruptedException ex2) {
throw new IOException(ex2);
}
}
}
});
}
}

// private method for deleting every subfiles/subdirectories of a file object
Expand Down
Loading