use of org.h2.store.FileLock in project h2database by h2database.
the class TestFileSystem method testSimple.
private void testSimple(final String fsBase) throws Exception {
long time = System.currentTimeMillis();
for (String s : FileUtils.newDirectoryStream(fsBase)) {
FileUtils.delete(s);
}
FileUtils.createDirectories(fsBase + "/test");
assertTrue(FileUtils.exists(fsBase));
FileUtils.delete(fsBase + "/test");
FileUtils.delete(fsBase + "/test2");
assertTrue(FileUtils.createFile(fsBase + "/test"));
List<FilePath> p = FilePath.get(fsBase).newDirectoryStream();
assertEquals(1, p.size());
String can = FilePath.get(fsBase + "/test").toRealPath().toString();
assertEquals(can, p.get(0).toString());
assertTrue(FileUtils.canWrite(fsBase + "/test"));
FileChannel channel = FileUtils.open(fsBase + "/test", "rw");
byte[] buffer = new byte[10000];
Random random = new Random(1);
random.nextBytes(buffer);
channel.write(ByteBuffer.wrap(buffer));
assertEquals(10000, channel.size());
channel.position(20000);
assertEquals(20000, channel.position());
assertEquals(-1, channel.read(ByteBuffer.wrap(buffer, 0, 1)));
String path = fsBase + "/test";
assertEquals("test", FileUtils.getName(path));
can = FilePath.get(fsBase).toRealPath().toString();
String can2 = FileUtils.toRealPath(FileUtils.getParent(path));
assertEquals(can, can2);
FileLock lock = channel.tryLock();
if (lock != null) {
lock.release();
}
assertEquals(10000, channel.size());
channel.close();
assertEquals(10000, FileUtils.size(fsBase + "/test"));
channel = FileUtils.open(fsBase + "/test", "r");
final byte[] test = new byte[10000];
FileUtils.readFully(channel, ByteBuffer.wrap(test, 0, 10000));
assertEquals(buffer, test);
final FileChannel fc = channel;
new AssertThrows(IOException.class) {
@Override
public void test() throws Exception {
fc.write(ByteBuffer.wrap(test, 0, 10));
}
};
new AssertThrows(NonWritableChannelException.class) {
@Override
public void test() throws Exception {
fc.truncate(10);
}
};
channel.close();
long lastMod = FileUtils.lastModified(fsBase + "/test");
if (lastMod < time - 1999) {
// at most 2 seconds difference
assertEquals(time, lastMod);
}
assertEquals(10000, FileUtils.size(fsBase + "/test"));
List<String> list = FileUtils.newDirectoryStream(fsBase);
assertEquals(1, list.size());
assertTrue(list.get(0).endsWith("test"));
IOUtils.copyFiles(fsBase + "/test", fsBase + "/test3");
FileUtils.move(fsBase + "/test3", fsBase + "/test2");
FileUtils.move(fsBase + "/test2", fsBase + "/test2");
assertFalse(FileUtils.exists(fsBase + "/test3"));
assertTrue(FileUtils.exists(fsBase + "/test2"));
assertEquals(10000, FileUtils.size(fsBase + "/test2"));
byte[] buffer2 = new byte[10000];
InputStream in = FileUtils.newInputStream(fsBase + "/test2");
int pos = 0;
while (true) {
int l = in.read(buffer2, pos, Math.min(10000 - pos, 1000));
if (l <= 0) {
break;
}
pos += l;
}
in.close();
assertEquals(10000, pos);
assertEquals(buffer, buffer2);
assertTrue(FileUtils.tryDelete(fsBase + "/test2"));
FileUtils.delete(fsBase + "/test");
if (fsBase.indexOf("memFS:") < 0 && fsBase.indexOf("memLZF:") < 0 && fsBase.indexOf("nioMemFS:") < 0 && fsBase.indexOf("nioMemLZF:") < 0) {
FileUtils.createDirectories(fsBase + "/testDir");
assertTrue(FileUtils.isDirectory(fsBase + "/testDir"));
if (!fsBase.startsWith("jdbc:")) {
FileUtils.deleteRecursive(fsBase + "/testDir", false);
assertFalse(FileUtils.exists(fsBase + "/testDir"));
}
}
}
use of org.h2.store.FileLock in project h2database by h2database.
the class TestFileLock method run.
@Override
public void run() {
FileLock lock = null;
while (!stop) {
lock = new FileLock(new TraceSystem(null), getFile(), 100);
try {
lock.lock(allowSockets ? FileLockMethod.SOCKET : FileLockMethod.FILE);
base.trace(lock + " locked");
locks++;
if (locks > 1) {
System.err.println("ERROR! LOCKS=" + locks + " sockets=" + allowSockets);
stop = true;
}
Thread.sleep(wait + (int) (Math.random() * wait));
locks--;
base.trace(lock + " unlock");
lock.unlock();
if (locks < 0) {
System.err.println("ERROR! LOCKS=" + locks);
stop = true;
}
} catch (Exception e) {
// log(id+" cannot lock: " + e);
}
try {
Thread.sleep(wait + (int) (Math.random() * wait));
} catch (InterruptedException e1) {
// ignore
}
}
if (lock != null) {
lock.unlock();
}
}
use of org.h2.store.FileLock in project h2database by h2database.
the class TestFileLock method testSimple.
private void testSimple() {
FileLock lock1 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
FileLock lock2 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
lock1.lock(FileLockMethod.FILE);
createClassProxy(FileLock.class);
assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, lock2).lock(FileLockMethod.FILE);
lock1.unlock();
lock2 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
lock2.lock(FileLockMethod.FILE);
lock2.unlock();
}
Aggregations