Search in sources :

Example 1 with LockTimeoutException

use of org.eclipse.tycho.locking.facade.LockTimeoutException in project tycho by eclipse.

the class FileLockServiceTest method testLockedByOtherProcess.

@Test
public void testLockedByOtherProcess() throws Exception {
    File testFile = newTestFile();
    FileLocker locker = subject.getFileLocker(testFile);
    LockProcess lockProcess = new LockProcess(testFile, 200L);
    lockProcess.lockFileInForkedProcess();
    assertTrue(locker.isLocked());
    try {
        locker.lock(0L);
        fail("lock already held by other VM but could be acquired a second time");
    } catch (LockTimeoutException e) {
    // expected
    }
    lockProcess.cleanup();
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) File(java.io.File) LockTimeoutException(org.eclipse.tycho.locking.facade.LockTimeoutException) Test(org.junit.Test)

Example 2 with LockTimeoutException

use of org.eclipse.tycho.locking.facade.LockTimeoutException in project tycho by eclipse.

the class FileLockerImpl method lock.

@Override
public void lock(long timeout) {
    if (timeout < 0) {
        throw new IllegalArgumentException("timeout must not be negative");
    }
    boolean success = false;
    final long waitInterval = 50L;
    long maxTries = (timeout / waitInterval) + 1;
    IOException ioException = null;
    for (long i = 0; i < maxTries; i++) {
        ioException = null;
        try {
            success = lockFileLocation.lock();
        } catch (IOException ioe) {
            // keep trying (and re-throw eventually)
            ioException = ioe;
        }
        if (success) {
            return;
        }
        try {
            Thread.sleep(waitInterval);
        } catch (InterruptedException e) {
        // ignore
        }
    }
    String message = "lock timeout: Could not acquire lock on file " + lockFileLocation.getURL() + " for " + timeout + " msec";
    if (ioException != null) {
        throw new LockTimeoutException(message, ioException);
    } else {
        throw new LockTimeoutException(message);
    }
}
Also used : IOException(java.io.IOException) LockTimeoutException(org.eclipse.tycho.locking.facade.LockTimeoutException)

Example 3 with LockTimeoutException

use of org.eclipse.tycho.locking.facade.LockTimeoutException in project tycho by eclipse.

the class FileLockServiceTest method testLockReentranceSameLocker.

@Test
public void testLockReentranceSameLocker() throws IOException {
    FileLocker fileLocker = subject.getFileLocker(newTestFile());
    fileLocker.lock();
    try {
        // locks are not re-entrant
        fileLocker.lock(0L);
        fail("lock already held by same VM but could be acquired a second time");
    } catch (LockTimeoutException e) {
    // expected
    } finally {
        fileLocker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) LockTimeoutException(org.eclipse.tycho.locking.facade.LockTimeoutException) Test(org.junit.Test)

Example 4 with LockTimeoutException

use of org.eclipse.tycho.locking.facade.LockTimeoutException in project tycho by eclipse.

the class FileLockServiceTest method testLockReentranceDifferentLocker.

@Test
public void testLockReentranceDifferentLocker() throws IOException {
    final File testFile = newTestFile();
    FileLocker fileLocker1 = subject.getFileLocker(testFile);
    FileLocker fileLocker2 = subject.getFileLocker(testFile);
    // same file but different locker objects
    assertNotSame(fileLocker1, fileLocker2);
    fileLocker1.lock();
    try {
        fileLocker2.lock(0L);
        fail("lock already held by same VM but could be acquired a second time");
    } catch (LockTimeoutException e) {
    // expected
    } finally {
        fileLocker1.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) File(java.io.File) LockTimeoutException(org.eclipse.tycho.locking.facade.LockTimeoutException) Test(org.junit.Test)

Aggregations

LockTimeoutException (org.eclipse.tycho.locking.facade.LockTimeoutException)4 FileLocker (org.eclipse.tycho.locking.facade.FileLocker)3 Test (org.junit.Test)3 File (java.io.File)2 IOException (java.io.IOException)1