Add file locking, work on #229

This commit is contained in:
Per Minborg
2020-09-02 17:40:18 +02:00
parent 5ace988277
commit 36f03b4e4a
7 changed files with 455 additions and 10 deletions
@@ -0,0 +1,108 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleFileLockException;
import net.openhft.chronicle.hash.impl.util.CanonicalRandomAccessFiles;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class FileLockUtilTest {
private File canonicalFile;
private RandomAccessFile raf;
private FileChannel fileChannel;
@Before
public void setUp() throws IOException {
canonicalFile = new File("file.lock").getCanonicalFile();
canonicalFile.delete();
canonicalFile.createNewFile();
raf = CanonicalRandomAccessFiles.acquire(canonicalFile);
fileChannel = raf.getChannel();
}
@After
public void cleanup() throws IOException {
fileChannel.close();
CanonicalRandomAccessFiles.release(canonicalFile);
}
@Test
public void testShared() {
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
FileLockUtil.releaseFileLock(canonicalFile);
FileLockUtil.releaseFileLock(canonicalFile);
}
@Test
public void testExclusiveNormalCase() {
FileLockUtil.acquireExclusiveFileLock(canonicalFile, fileChannel);
FileLockUtil.releaseFileLock(canonicalFile);
FileLockUtil.acquireExclusiveFileLock(canonicalFile, fileChannel);
FileLockUtil.releaseFileLock(canonicalFile);
}
@Test
public void testTryExclusiveButWasShared() {
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
try {
FileLockUtil.acquireExclusiveFileLock(canonicalFile, fileChannel);
fail();
} catch (ChronicleFileLockException ignore) {
}
FileLockUtil.releaseFileLock(canonicalFile);
}
@Test
public void testTrySharedButWasExclusive() {
FileLockUtil.acquireExclusiveFileLock(canonicalFile, fileChannel);
try {
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
fail();
} catch (ChronicleFileLockException ignore) {
}
FileLockUtil.releaseFileLock(canonicalFile);
}
@Test
public void testComplicated() {
FileLockUtil.acquireExclusiveFileLock(canonicalFile, fileChannel);
FileLockUtil.releaseFileLock(canonicalFile);
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
FileLockUtil.releaseFileLock(canonicalFile);
FileLockUtil.releaseFileLock(canonicalFile);
FileLockUtil.acquireExclusiveFileLock(canonicalFile, fileChannel);
FileLockUtil.releaseFileLock(canonicalFile);
}
@Test
public void testRunExclusively() {
FileLockUtil.dump();
final AtomicInteger cnt = new AtomicInteger();
FileLockUtil.runExclusively(canonicalFile, fileChannel, cnt::incrementAndGet);
assertEquals(1, cnt.get());
}
@Test
public void testRunExclusivelyButUsed() {
FileLockUtil.acquireSharedFileLock(canonicalFile, fileChannel);
try {
FileLockUtil.runExclusively(canonicalFile, fileChannel, () -> {});
fail();
} catch (ChronicleFileLockException e) {
FileLockUtil.releaseFileLock(canonicalFile);
}
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 2012-2018 Chronicle Map Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashRecoveryFailedException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class Issue229Test {
private File mapFile;
@Before
public void setup() {
mapFile = new File("test_map");
}
@After
public void cleanup() {
mapFile.delete();
}
@Test(expected = ChronicleHashRecoveryFailedException.class)
public void assureExclusiveAccess() throws IOException {
try (ChronicleMap<Long, Long> readMap = ChronicleMap
.of(Long.class, Long.class)
.entries(10)
.createPersistedTo(mapFile)) {
// It shall not be possible to recover since the
// file is open by the readMap
try (ChronicleMap<Long, Long> recoverMap = ChronicleMap
.of(Long.class, Long.class)
.entries(10)
.recoverPersistedTo(mapFile, true)) {
}
}
}
}