use of org.glassfish.common.util.admin.ManagedFile in project Payara by payara.
the class FileLockTest method timeOutTest.
@Test
public void timeOutTest() throws Exception {
final File f = getFile();
final ManagedFile managed = new ManagedFile(f, 1000, 10000);
Lock fl;
try {
fl = managed.accessWrite();
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
System.out.println("Obtained first lock, waiting about 500 for secondary lock to timeout...");
try {
Executors.newFixedThreadPool(2).submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
long now = System.currentTimeMillis();
ManagedFile m = new ManagedFile(f, 500, 1000);
try {
Lock lock = m.accessRead();
lock.unlock();
throw new RuntimeException("Test failed, got the lock that should have timed out");
} catch (TimeoutException e) {
System.out.println("Great, got timed out after " + (System.currentTimeMillis() - now));
}
return null;
}
}).get();
// let's check we also cannot get the write lock...
ManagedFile m = new ManagedFile(f, 100, 100);
try {
Lock lock = m.accessWrite();
lock.unlock();
throw new RuntimeException("Test failed, got the write lock that should have timed out");
} catch (TimeoutException e) {
System.out.println("Even better, got timed out trying to get another write lock");
}
fl.unlock();
} catch (Exception e) {
e.printStackTrace();
fl.unlock();
}
}
Aggregations