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();
}
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);
}
}
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();
}
}
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();
}
}
Aggregations