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

[IO-856] test for background deletion #697

Closed
wants to merge 19 commits 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
55 changes: 54 additions & 1 deletion src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.file.PathUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.function.Uncheck;
Expand Down Expand Up @@ -211,7 +219,9 @@ public void testListFilesWithDeletion() throws IOException {
final String[] extensions = {"xml", "txt"};
final List<File> list;
final File xFile = new File(temporaryFolder, "x.xml");
xFile.createNewFile();
if (!xFile.createNewFile()) {
fail("could not create test file: " + xFile);
thhart marked this conversation as resolved.
Show resolved Hide resolved
}
final Collection<File> files = FileUtils.listFiles(temporaryFolder, extensions, true);
assertEquals(5, files.size());
try (Stream<File> stream = Uncheck.get(() -> FileUtils.streamFiles(temporaryFolder, true, extensions))) {
Expand All @@ -222,4 +232,47 @@ public void testListFilesWithDeletion() throws IOException {
assertEquals(4, list.size());
}

@Test
public void testListFilesWithDeletionThreaded() throws ExecutionException, InterruptedException {
// test for IO-856
// create random directory in tmp, create the directory if it does not exist
final Path tempPath = PathUtils.getTempDirectory().resolve("IO-856");
final File tempDir = tempPath.toFile();
if (!tempDir.exists() && !tempDir.mkdirs()) {
fail("Could not create file path: " + tempDir.getAbsolutePath());
}
final int waitTime = 10_000;
final byte[] bytes = "TEST".getBytes(StandardCharsets.UTF_8);
final CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
final long endTime = System.currentTimeMillis() + waitTime;
while (System.currentTimeMillis() < endTime) {
final File file = new File(tempDir.getAbsolutePath(), UUID.randomUUID() + ".deletetester");
file.deleteOnExit();
try {
Files.write(file.toPath(), bytes);
} catch (final Exception e) {
fail("Could not create test file: " + file.getAbsolutePath(), e);
}
if (!file.delete()) {
fail("Could not delete test file: " + file.getAbsolutePath());
}
}
});

final CompletableFuture<Void> c2 = CompletableFuture.runAsync(() -> {
final long endTime = System.currentTimeMillis() + waitTime;
try {
while (System.currentTimeMillis() < endTime) {
FileUtils.listFiles(tempDir, new String[] { "\\.deletetester" }, false);
}
} catch (final Exception e) {
fail("Should not happen", e);
}
});

// wait for the threads to finish
c1.get();
c2.get();
}

}
Loading