Skip to content

Commit ed036b0

Browse files
committed
fix: mitigate random test failures on Windows because of locked resource
1 parent a9e9269 commit ed036b0

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/AbstractTestWithProject.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import org.eclipse.core.resources.IProject;
1717
import org.eclipse.core.resources.IResource;
18+
import org.eclipse.core.runtime.CoreException;
1819
import org.junit.jupiter.api.AfterEach;
1920
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.TestInfo;
@@ -35,8 +36,38 @@ public void setUpProject(TestInfo testInfo) throws Exception {
3536

3637
@AfterEach
3738
public void tearDownProject() throws Exception {
38-
if (project != null) {
39-
project.delete(IResource.FORCE, null);
39+
if (project != null && project.exists()) {
40+
deleteProjectWithRetries(project, 10, 500);
41+
}
42+
}
43+
44+
/**
45+
* Mitigation for potential
46+
* <code>java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process</code>
47+
* when deleting a project.
48+
*/
49+
private static void deleteProjectWithRetries(IProject project, int maxAttempts, long delayMillis)
50+
throws CoreException {
51+
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
52+
try {
53+
if (!project.exists()) {
54+
break;
55+
}
56+
project.close(null);
57+
project.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, null);
58+
break;
59+
} catch (CoreException ex) {
60+
if (attempt == maxAttempts) {
61+
throw ex;
62+
}
63+
try {
64+
Thread.sleep(delayMillis);
65+
} catch (InterruptedException ie) {
66+
Thread.currentThread().interrupt();
67+
ex.printStackTrace();
68+
break;
69+
}
70+
}
4071
}
4172
}
4273
}

0 commit comments

Comments
 (0)