1 package org.apache.archiva.common.filelock;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import edu.umd.cs.mtc.MultithreadedTestCase;
23 import edu.umd.cs.mtc.TestFramework;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.test.context.ContextConfiguration;
31 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33 import javax.inject.Inject;
34 import javax.inject.Named;
36 import java.io.FileNotFoundException;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.nio.file.FileAlreadyExistsException;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.nio.file.StandardCopyOption;
44 import java.util.concurrent.atomic.AtomicInteger;
46 //import org.apache.commons.io.IOUtils;
49 * @author Olivier Lamy
51 @RunWith(SpringJUnit4ClassRunner.class)
52 @ContextConfiguration(locations = {"classpath*:/META-INF/spring-context.xml"})
53 public class DefaultFileLockManagerTest {
55 final Logger logger = LoggerFactory.getLogger(getClass());
58 @Named(value = "fileLockManager#default")
59 FileLockManager fileLockManager;
61 class ConcurrentFileWrite
62 extends MultithreadedTestCase {
65 AtomicInteger success = new AtomicInteger(0);
67 FileLockManager fileLockManager;
69 File file = new File(System.getProperty("buildDirectory"), "foo.txt");
71 File largeJar = new File(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar");
73 ConcurrentFileWrite(FileLockManager fileLockManager)
75 this.fileLockManager = fileLockManager;
76 //file.createNewFile();
81 public void initialize() {
85 // Files.copy is not atomic so have to try several times in
86 // a multithreaded test
87 private void copyFile(Path source, Path destination) {
89 boolean finished = false;
90 while (!finished && attempts-- > 0) {
92 Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING,
93 StandardCopyOption.COPY_ATTRIBUTES);
95 } catch (IOException ex) {
101 public void thread1()
102 throws FileLockException, FileLockTimeoutException, IOException {
104 logger.info("thread1");
105 Lock lock = fileLockManager.writeFileLock(this.file);
107 lock.getFile().delete();
108 copyFile(largeJar.toPath(), lock.getFile().toPath());
110 fileLockManager.release(lock);
112 logger.info("thread1 ok");
113 success.incrementAndGet();
114 } catch (Throwable e) {
115 logger.error("Error occured {}", e.getMessage());
121 public void thread2()
122 throws FileLockException, FileLockTimeoutException, IOException {
124 logger.info("thread2");
125 Lock lock = fileLockManager.writeFileLock(this.file);
127 lock.getFile().delete();
128 copyFile(largeJar.toPath(), lock.getFile().toPath());
130 fileLockManager.release(lock);
132 logger.info("thread2 ok");
133 success.incrementAndGet();
134 } catch (Throwable e) {
135 logger.error("Error occured {}", e.getMessage());
142 public void thread3()
143 throws FileLockException, FileLockTimeoutException, IOException {
145 logger.info("thread3");
146 Lock lock = fileLockManager.readFileLock(this.file);
149 outFile = Files.createTempFile("foo", ".jar");
150 Files.copy(Paths.get(lock.getFile().getPath()),
151 Files.newOutputStream(outFile));
153 fileLockManager.release(lock);
154 if (outFile!=null) Files.delete( outFile );
156 logger.info("thread3 ok");
157 success.incrementAndGet();
158 } catch (Throwable e) {
159 logger.error("Error occured {}", e.getMessage());
166 public void thread4()
167 throws FileLockException, FileLockTimeoutException, IOException {
169 logger.info("thread4");
170 Lock lock = fileLockManager.writeFileLock(this.file);
172 lock.getFile().delete();
173 copyFile(largeJar.toPath(), lock.getFile().toPath());
175 fileLockManager.release(lock);
177 logger.info("thread4 ok");
178 success.incrementAndGet();
179 } catch (Throwable e) {
180 logger.error("Error occured {}", e.getMessage());
187 public void thread5()
188 throws FileLockException, FileLockTimeoutException, IOException {
190 logger.info("thread5");
191 Lock lock = fileLockManager.writeFileLock(this.file);
193 lock.getFile().delete();
194 copyFile(largeJar.toPath(), lock.getFile().toPath());
196 fileLockManager.release(lock);
198 logger.info("thread5 ok");
199 success.incrementAndGet();
200 } catch (Throwable e) {
201 logger.error("Error occured {}", e.getMessage());
208 public void thread6()
209 throws FileLockException, FileLockTimeoutException, IOException {
211 logger.info("thread6");
212 Lock lock = fileLockManager.readFileLock(this.file);
215 outFile = Files.createTempFile("foo", ".jar");
216 Files.copy(lock.getFile().toPath(), Files.newOutputStream( outFile ));
218 fileLockManager.release(lock);
219 if (outFile!=null) Files.delete( outFile );
221 logger.info("thread6 ok");
222 success.incrementAndGet();
223 } catch (Throwable e) {
224 logger.error("Error occured {}", e.getMessage());
231 public void thread7()
232 throws FileLockException, FileLockTimeoutException, IOException {
234 logger.info("thread7");
235 Lock lock = fileLockManager.writeFileLock(this.file);
237 lock.getFile().delete();
238 copyFile(largeJar.toPath(), lock.getFile().toPath());
240 fileLockManager.release(lock);
242 logger.info("thread7 ok");
243 success.incrementAndGet();
244 } catch (Throwable e) {
245 logger.error("Error occured {}", e.getMessage());
252 public void thread8()
253 throws FileLockException, FileLockTimeoutException, IOException {
255 logger.info("thread8");
256 Lock lock = fileLockManager.readFileLock(this.file);
259 outFile = Files.createTempFile("foo", ".jar");
260 Files.copy(lock.getFile().toPath(), Files.newOutputStream( outFile ));
262 fileLockManager.release(lock);
263 if (outFile!=null) Files.delete( outFile );
265 logger.info("thread8 ok");
266 success.incrementAndGet();
267 } catch (Throwable e) {
268 logger.error("Error occured {}", e.getMessage());
275 public void thread9()
276 throws FileLockException, FileLockTimeoutException, IOException {
278 logger.info("thread9");
279 Lock lock = fileLockManager.writeFileLock(this.file);
281 lock.getFile().delete();
282 copyFile(largeJar.toPath(), lock.getFile().toPath());
284 fileLockManager.release(lock);
286 logger.info("thread9 ok");
287 success.incrementAndGet();
288 } catch (Throwable e) {
289 logger.error("Error occured {}", e.getMessage());
295 public void thread10()
296 throws FileLockException, FileLockTimeoutException, IOException {
298 logger.info("thread10");
299 Lock lock = fileLockManager.readFileLock(this.file);
302 outFile = Files.createTempFile("foo", ".jar");
303 Files.copy(lock.getFile().toPath(), Files.newOutputStream( outFile ));
305 fileLockManager.release(lock);
306 if (outFile!=null) Files.delete(outFile);
308 logger.info("thread10 ok");
309 success.incrementAndGet();
310 } catch (Throwable e) {
311 logger.error("Error occured {}", e.getMessage());
323 public void initialize() {
324 fileLockManager.setSkipLocking(false);
325 fileLockManager.clearLockFiles();
329 public void testWrite()
331 ConcurrentFileWrite concurrentFileWrite = new ConcurrentFileWrite(fileLockManager);
332 //concurrentFileWrite.setTrace( true );
333 TestFramework.runManyTimes(concurrentFileWrite, 10, TestFramework.DEFAULT_CLOCKPERIOD, 20);
334 logger.info("success: {}", concurrentFileWrite.success);
335 Assert.assertEquals(100, concurrentFileWrite.success.intValue());