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

Fix deleting temp directories #6187

Merged
merged 1 commit into from Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion utils/src/main/java/org/robolectric/util/TempDirectory.java
@@ -1,5 +1,7 @@
package org.robolectric.util;

import static java.util.concurrent.TimeUnit.SECONDS;

import java.io.IOException;
import java.io.Writer;
import java.nio.file.FileAlreadyExistsException;
Expand Down Expand Up @@ -50,14 +52,21 @@ public TempDirectory(String name) {
}
}

private static void clearAllDirectories() {
static void clearAllDirectories() {
ExecutorService deletionExecutorService = Executors.newFixedThreadPool(DELETE_THREAD_POOL_SIZE);
synchronized (tempDirectoriesToDelete) {
for (TempDirectory undeletedDirectory : tempDirectoriesToDelete) {
deletionExecutorService.execute(undeletedDirectory::destroy);
}
}
deletionExecutorService.shutdown();
try {
deletionExecutorService.awaitTermination(5, SECONDS);
} catch (InterruptedException e) {
deletionExecutorService.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}

public Path createFile(String name, String contents) {
Expand Down
11 changes: 11 additions & 0 deletions utils/src/test/java/org/robolectric/util/TempDirectoryTest.java
Expand Up @@ -18,4 +18,15 @@ public void createsDirsWithSameParent() throws IOException {
Path path2 = tempDir.create("dir2");
assertThat(path.getParent().toString()).isEqualTo(path2.getParent().toString());
}

@Test
public void clearAllDirectories_removesDirectories() {
TempDirectory tempDir = new TempDirectory("temp_dir");
Path dir = tempDir.create("dir1");
Path file = tempDir.create("file1");
TempDirectory.clearAllDirectories();
assertThat(dir.toFile().exists()).isFalse();
assertThat(file.toFile().exists()).isFalse();
assertThat(dir.getParent().toFile().exists()).isFalse();
}
}