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 1 commit
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
63 changes: 52 additions & 11 deletions src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
*/
package org.apache.commons.io;

import static org.junit.jupiter.api.Assertions.assertEquals;
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 org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.function.Uncheck;
import org.apache.commons.lang3.function.Consumers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,13 +33,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.function.Uncheck;
import org.apache.commons.lang3.function.Consumers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.*;
thhart marked this conversation as resolved.
Show resolved Hide resolved

/**
* Tests FileUtils.listFiles() methods.
Expand Down Expand Up @@ -222,4 +219,48 @@ public void testListFilesWithDeletion() throws IOException {
assertEquals(4, list.size());
}

@Test
public void testListFilesWithDeletionThreaded() throws java.util.concurrent.ExecutionException, InterruptedException {
thhart marked this conversation as resolved.
Show resolved Hide resolved
// test for IO-856
// create random directory in tmp, create the directory if it does not exist
final File dir = FileUtils.getTempDirectory();
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new RuntimeException("could not create image file path: " + dir.getAbsolutePath());
thhart marked this conversation as resolved.
Show resolved Hide resolved
}
}
final int waitTime = 10000;
final java.util.concurrent.CompletableFuture<Void> c1 = java.util.concurrent.CompletableFuture.runAsync(() -> {
long endTime = System.currentTimeMillis() + waitTime;
while (System.currentTimeMillis() < endTime) {
final File file = new File(dir.getAbsolutePath(), java.util.UUID.randomUUID() + ".deletetester");
file.deleteOnExit();
try {
new java.io.BufferedOutputStream(java.nio.file.Files.newOutputStream(file.toPath())).write("TEST".getBytes(java.nio.charset.StandardCharsets.UTF_8));
thhart marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
throw new RuntimeException("could not create test file: " + file.getAbsolutePath(), e);
thhart marked this conversation as resolved.
Show resolved Hide resolved
}
if (!file.delete()) {
throw new RuntimeException("could not delete test file: " + file.getAbsolutePath());
}
}
});

final java.util.concurrent.CompletableFuture<Void> c2 = java.util.concurrent.CompletableFuture.runAsync(() -> {
thhart marked this conversation as resolved.
Show resolved Hide resolved
long endTime = System.currentTimeMillis() + waitTime;
try {
while (System.currentTimeMillis() < endTime) {
final Collection<File> files = FileUtils.listFiles(dir, new String[]{"\\.deletetester"}, false);
files.size();
thhart marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});

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

}