Search in sources :

Example 11 with ManagedFile

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();
    }
}
Also used : ManagedFile(org.glassfish.common.util.admin.ManagedFile) ManagedFile(org.glassfish.common.util.admin.ManagedFile) URISyntaxException(java.net.URISyntaxException) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Aggregations

ManagedFile (org.glassfish.common.util.admin.ManagedFile)11 URISyntaxException (java.net.URISyntaxException)9 Lock (java.util.concurrent.locks.Lock)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)4 File (java.io.File)2 IOException (java.io.IOException)2 Random (java.util.Random)2 Callable (java.util.concurrent.Callable)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1