StorageLock has lock / unlock for default StorageLockContext

This commit is contained in:
Maxim.Mossienko
2012-05-21 12:57:18 +04:00
parent 7a30c7661d
commit ce9db7e26b
2 changed files with 29 additions and 5 deletions
@@ -109,6 +109,14 @@ public class PagedFileStorage implements Forceable {
myDefaultStorageLockContext = new StorageLockContext(this);
}
public void lock() {
myDefaultStorageLockContext.myReentrantLock.lock();
}
public void unlock() {
myDefaultStorageLockContext.myReentrantLock.unlock();
}
private final BuffersCache myBuffersCache = new BuffersCache();
private final ConcurrentHashMap<Integer, PagedFileStorage> myIndex2Storage = new ConcurrentHashMap<Integer, PagedFileStorage>();
@@ -32,25 +32,33 @@ public class PagedFileStorageTest extends TestCase {
@Override
public void setUp() throws Exception {
super.setUp();
synchronized (lock) {
lock.lock();
try {
f = FileUtil.createTempFile("storage", ".tmp");
s = new PagedFileStorage(f, lock);
}
finally {
lock.unlock();
}
}
@Override
public void tearDown() throws Exception {
synchronized (lock) {
lock.lock();
try {
s.close();
final File l = new File(f.getPath() + ".len");
assert !l.exists() || l.delete() : l.getPath();
assert f.delete() : f.getPath();
} finally {
lock.unlock();
}
super.tearDown();
}
public void testResizing() throws IOException {
synchronized (lock) {
lock.lock();
try {
assertEquals(0, f.length());
s.resize(12345);
@@ -58,21 +66,27 @@ public class PagedFileStorageTest extends TestCase {
s.resize(123);
assertEquals(123, f.length());
} finally {
lock.unlock();
}
}
public void testFillingWithZerosAfterResize() throws IOException {
synchronized (lock) {
lock.lock();
try {
s.resize(1000);
for (int i = 0; i < 1000; i++) {
assertEquals(0, s.get(i));
}
} finally {
lock.unlock();
}
}
public void testResizeableMappedFile() throws Exception {
synchronized (lock) {
lock.lock();
try {
ResizeableMappedFile file = new ResizeableMappedFile(f, 2000000, lock);
System.out.println("writing...");
@@ -99,6 +113,8 @@ public class PagedFileStorageTest extends TestCase {
System.out.println("done in " + t + " ms");
file.close();
} finally {
lock.unlock();
}
}