aboutsummaryrefslogtreecommitdiffstats
blob: 9dc09b46ff9986acc2b69af7d12d6be6507bc404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*******************************************************************************
 * Copyright (c) 2011 SAP AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     SAP AG - initial API and implementation
 *******************************************************************************/

package org.eclipse.tycho.core.locking;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import org.eclipse.tycho.locking.facade.FileLockService;
import org.eclipse.tycho.locking.facade.FileLocker;
import org.eclipse.tycho.locking.facade.LockTimeoutException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class FileLockServiceTest {

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();
    private FileLockService subject;

    @Before
    public void setup() {
        subject = new FileLockServiceImpl();
    }

    @Test
    public void testIsLocked() throws IOException {
        FileLocker fileLocker = subject.getFileLocker(newTestFile());
        assertFalse(fileLocker.isLocked());
        fileLocker.lock();
        try {
            assertTrue(fileLocker.isLocked());
        } finally {
            fileLocker.release();
            assertFalse(fileLocker.isLocked());
        }
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNegativeTimeout() throws IOException {
        FileLocker fileLocker = subject.getFileLocker(newTestFile());
        fileLocker.lock(-1L);
    }

    @Test
    public void testLockDirectory() throws IOException {
        File testDir = tempFolder.newFolder("test");
        FileLockerImpl fileLocker = (FileLockerImpl) subject.getFileLocker(testDir);
        fileLocker.lock();
        try {
            assertTrue(fileLocker.isLocked());
            assertEquals(new File(testDir, ".tycholock").getCanonicalPath(),
                    fileLocker.lockMarkerFile.getCanonicalPath());
        } finally {
            fileLocker.release();
        }
    }

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

    @Test
    public void testReuseLockerObject() throws IOException {
        FileLocker fileLocker = subject.getFileLocker(newTestFile());
        lockAndRelease(fileLocker);
        lockAndRelease(fileLocker);
    }

    private void lockAndRelease(FileLocker fileLocker) {
        assertFalse(fileLocker.isLocked());
        fileLocker.lock();
        assertTrue(fileLocker.isLocked());
        fileLocker.release();
        assertFalse(fileLocker.isLocked());
    }

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

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

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

    @Test
    public void testRelease() throws Exception {
        FileLocker locker = subject.getFileLocker(newTestFile());
        assertFalse(locker.isLocked());
        // releasing without holding the lock should do nothing
        locker.release();
    }

    @Test
    public void testMarkerFileDeletion() throws Exception {
        FileLockerImpl locker = (FileLockerImpl) subject.getFileLocker(newTestFile());
        locker.lock();
        assertTrue(locker.lockMarkerFile.isFile());
        locker.release();
        assertFalse(locker.lockMarkerFile.isFile());
    }

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

    private File newTestFile() throws IOException {
        File testFile = tempFolder.newFile("testfile-" + new Random().nextInt());
        return testFile;
    }

}