Search in sources :

Example 1 with FileLocker

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

the class FileLockServiceTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    File testFile = newTestFile();
    FileLocker locker = subject.getFileLocker(testFile);
    long waitTime = 1000L;
    LockProcess lockProcess = new LockProcess(testFile, waitTime);
    long start = System.currentTimeMillis();
    lockProcess.lockFileInForkedProcess();
    locker.lock(20000L);
    try {
        long duration = System.currentTimeMillis() - start;
        assertTrue(duration >= waitTime);
    } finally {
        lockProcess.cleanup();
        locker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) File(java.io.File) Test(org.junit.Test)

Example 2 with FileLocker

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

the class FileLockServiceTest method testURLEncoding.

@Test
public void testURLEncoding() throws IOException {
    File testFile = new File(tempFolder.getRoot(), "file with spaces" + new Random().nextInt());
    File markerFile = new File(testFile.getAbsolutePath() + ".tycholock");
    FileLocker fileLocker = subject.getFileLocker(testFile);
    assertFalse(markerFile.isFile());
    fileLocker.lock();
    try {
        assertTrue(markerFile.isFile());
    } finally {
        fileLocker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) Random(java.util.Random) File(java.io.File) Test(org.junit.Test)

Example 3 with FileLocker

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

the class FileLockServiceTest method testRelease.

@Test
public void testRelease() throws Exception {
    FileLocker locker = subject.getFileLocker(newTestFile());
    assertFalse(locker.isLocked());
    // releasing without holding the lock should do nothing
    locker.release();
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) Test(org.junit.Test)

Example 4 with FileLocker

use of org.eclipse.tycho.locking.facade.FileLocker 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 5 with FileLocker

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

the class LocalMavenRepositoryTool method filterLinesFromIndex.

private void filterLinesFromIndex(File indexFile, Set<String> toBeRemoved) throws UnsupportedEncodingException, FileNotFoundException, IOException {
    FileLocker locker = fileLockService.getFileLocker(indexFile);
    locker.lock();
    try {
        Set<String> currentLines = readLines(indexFile);
        currentLines.removeAll(toBeRemoved);
        writeLines(indexFile, currentLines);
    } finally {
        locker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker)

Aggregations

FileLocker (org.eclipse.tycho.locking.facade.FileLocker)17 File (java.io.File)9 Test (org.junit.Test)9 IOException (java.io.IOException)3 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)3 LockTimeoutException (org.eclipse.tycho.locking.facade.LockTimeoutException)3 JarFile (java.util.jar.JarFile)2 ZipFile (java.util.zip.ZipFile)2 Random (java.util.Random)1 JarEntry (java.util.jar.JarEntry)1 ZipEntry (java.util.zip.ZipEntry)1 MavenExecutionException (org.apache.maven.MavenExecutionException)1 Artifact (org.apache.maven.artifact.Artifact)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ZipUnArchiver (org.codehaus.plexus.archiver.zip.ZipUnArchiver)1 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)1 RawInputStreamFacade (org.codehaus.plexus.util.io.RawInputStreamFacade)1 ArtifactDescriptor (org.eclipse.tycho.ArtifactDescriptor)1 DependencyArtifacts (org.eclipse.tycho.artifacts.DependencyArtifacts)1