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.FileOutputStream;
37 import java.io.IOException;
38 import java.nio.file.FileAlreadyExistsException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.nio.file.StandardCopyOption;
43 import java.util.concurrent.atomic.AtomicInteger;
45 //import org.apache.commons.io.IOUtils;
48 * @author Olivier Lamy
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
52 public class DefaultFileLockManagerTest
55 final Logger logger = LoggerFactory.getLogger( getClass() );
58 @Named(value = "fileLockManager#default")
59 FileLockManager fileLockManager;
61 class ConcurrentFileWrite
62 extends MultithreadedTestCase
66 AtomicInteger success = new AtomicInteger( 0 );
68 FileLockManager fileLockManager;
70 File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" );
72 File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" );
74 ConcurrentFileWrite( FileLockManager fileLockManager )
77 this.fileLockManager = fileLockManager;
78 //file.createNewFile();
83 public void initialize()
88 // Files.copy is not atomic so have to try several times in
89 // a multithreaded test
90 private void copyFile(Path source, Path destination) {
92 boolean finished = false;
93 while(!finished && attempts-->0) {
95 Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING,
96 StandardCopyOption.COPY_ATTRIBUTES);
98 } catch (IOException ex) {
104 public void thread1()
105 throws FileLockException, FileLockTimeoutException, IOException
107 logger.info( "thread1" );
108 Lock lock = fileLockManager.writeFileLock( this.file );
111 lock.getFile().delete();
112 copyFile( largeJar.toPath(), lock.getFile().toPath());
116 fileLockManager.release( lock );
118 logger.info( "thread1 ok" );
119 success.incrementAndGet();
122 public void thread2()
123 throws FileLockException, FileLockTimeoutException, IOException
125 logger.info( "thread2" );
126 Lock lock = fileLockManager.writeFileLock( this.file );
129 lock.getFile().delete();
130 copyFile( largeJar.toPath(), lock.getFile().toPath());
134 fileLockManager.release( lock );
136 logger.info( "thread2 ok" );
137 success.incrementAndGet();
140 public void thread3()
141 throws FileLockException, FileLockTimeoutException, IOException
143 logger.info( "thread3" );
144 Lock lock = fileLockManager.readFileLock( this.file );
147 Files.copy( Paths.get( lock.getFile().getPath() ),
148 new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
152 fileLockManager.release( lock );
154 logger.info( "thread3 ok" );
155 success.incrementAndGet();
158 public void thread4()
159 throws FileLockException, FileLockTimeoutException, IOException
161 logger.info( "thread4" );
162 Lock lock = fileLockManager.writeFileLock( this.file );
165 lock.getFile().delete();
166 copyFile( largeJar.toPath(), lock.getFile().toPath());
170 fileLockManager.release( lock );
172 logger.info( "thread4 ok" );
173 success.incrementAndGet();
176 public void thread5()
177 throws FileLockException, FileLockTimeoutException, IOException
179 logger.info( "thread5" );
180 Lock lock = fileLockManager.writeFileLock( this.file );
183 lock.getFile().delete();
184 copyFile( largeJar.toPath(), lock.getFile().toPath());
188 fileLockManager.release( lock );
190 logger.info( "thread5 ok" );
191 success.incrementAndGet();
194 public void thread6()
195 throws FileLockException, FileLockTimeoutException, IOException
197 logger.info( "thread6" );
198 Lock lock = fileLockManager.readFileLock( this.file );
201 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
205 fileLockManager.release( lock );
207 logger.info( "thread6 ok" );
208 success.incrementAndGet();
211 public void thread7()
212 throws FileLockException, FileLockTimeoutException, IOException
214 logger.info( "thread7" );
215 Lock lock = fileLockManager.writeFileLock( this.file );
218 lock.getFile().delete();
219 copyFile( largeJar.toPath(), lock.getFile().toPath());
223 fileLockManager.release( lock );
225 logger.info( "thread7 ok" );
226 success.incrementAndGet();
229 public void thread8()
230 throws FileLockException, FileLockTimeoutException, IOException
232 logger.info( "thread8" );
233 Lock lock = fileLockManager.readFileLock( this.file );
236 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
240 fileLockManager.release( lock );
242 logger.info( "thread8 ok" );
243 success.incrementAndGet();
246 public void thread9()
247 throws FileLockException, FileLockTimeoutException, IOException
249 logger.info( "thread9" );
250 Lock lock = fileLockManager.writeFileLock( this.file );
253 lock.getFile().delete();
254 copyFile( largeJar.toPath(), lock.getFile().toPath());
258 fileLockManager.release( lock );
260 logger.info( "thread9 ok" );
261 success.incrementAndGet();
264 public void thread10()
265 throws FileLockException, FileLockTimeoutException, IOException
267 logger.info( "thread10" );
268 Lock lock = fileLockManager.readFileLock( this.file );
271 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
275 fileLockManager.release( lock );
277 logger.info( "thread10 ok" );
278 success.incrementAndGet();
286 public void initialize()
288 fileLockManager.setSkipLocking( false );
289 fileLockManager.clearLockFiles();
293 public void testWrite()
296 ConcurrentFileWrite concurrentFileWrite = new ConcurrentFileWrite( fileLockManager );
297 //concurrentFileWrite.setTrace( true );
298 TestFramework.runManyTimes( concurrentFileWrite, 10);
299 logger.info( "success: {}", concurrentFileWrite.success );
300 Assert.assertEquals( 100, concurrentFileWrite.success.intValue() );