Skip to content

Clear lock cache in NioFileLocker #2998

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

Merged
merged 1 commit into from
Jul 22, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
final class FileChannelCache {

private static ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<File, FileChannel>();
private static ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<>();


private FileChannelCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public class NioFileLocker extends AbstractFileLockerFilter {

private final ConcurrentMap<File, FileLock> lockCache = new ConcurrentHashMap<File, FileLock>();
private final ConcurrentMap<File, FileLock> lockCache = new ConcurrentHashMap<>();

@Override
public boolean lock(File fileToLock) {
Expand Down Expand Up @@ -68,7 +68,7 @@ public boolean isLockable(File file) {

@Override
public void unlock(File fileToUnlock) {
FileLock fileLock = this.lockCache.get(fileToUnlock);
FileLock fileLock = this.lockCache.remove(fileToUnlock);
try {
if (fileLock != null) {
fileLock.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.test.util.TestUtils;

/**
* @author Iwein Fuld
* @author Gary Russell
*/
public class NioFileLockerTests {

Expand All @@ -47,13 +50,18 @@ public void create() throws IOException {
};

@Test
public void fileListedByFirstFilter() throws IOException {
public void fileListedByFirstFilter() throws Exception {
NioFileLocker filter = new NioFileLocker();
File testFile = new File(workdir, "test0");
testFile.createNewFile();
assertThat(filter.filterFiles(workdir.listFiles()).get(0)).isEqualTo(testFile);
filter.lock(testFile);
assertThat(filter.filterFiles(workdir.listFiles()).get(0)).isEqualTo(testFile);
filter.unlock(testFile);
Field channelCache = FileChannelCache.class.getDeclaredField("channelCache");
channelCache.setAccessible(true);
assertThat(((Map<?, ?>) channelCache.get(null))).isEmpty();
assertThat(((Map<?, ?>) TestUtils.getPropertyValue(filter, "lockCache", Map.class))).isEmpty();
}

@Test
Expand All @@ -64,7 +72,8 @@ public void fileNotListedWhenLockedByOtherFilter() throws IOException {
testFile.createNewFile();
assertThat(filter1.filterFiles(workdir.listFiles()).get(0)).isEqualTo(testFile);
filter1.lock(testFile);
assertThat(filter2.filterFiles(workdir.listFiles())).isEqualTo((List<File>) new ArrayList<File>());
assertThat(filter2.filterFiles(workdir.listFiles())).isEqualTo(new ArrayList<File>());
filter1.unlock(testFile);
}

}