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.Files;
39 import java.nio.file.Paths;
40 import java.nio.file.StandardCopyOption;
41 import java.util.concurrent.atomic.AtomicInteger;
43 //import org.apache.commons.io.IOUtils;
46 * @author Olivier Lamy
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
50 public class DefaultFileLockManagerTest
53 final Logger logger = LoggerFactory.getLogger( getClass() );
56 @Named(value = "fileLockManager#default")
57 FileLockManager fileLockManager;
59 class ConcurrentFileWrite
60 extends MultithreadedTestCase
64 AtomicInteger success = new AtomicInteger( 0 );
66 FileLockManager fileLockManager;
68 File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" );
70 File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" );
72 ConcurrentFileWrite( FileLockManager fileLockManager )
75 this.fileLockManager = fileLockManager;
76 //file.createNewFile();
81 public void initialize()
87 throws FileLockException, FileLockTimeoutException, IOException
89 logger.info( "thread1" );
90 Lock lock = fileLockManager.writeFileLock( this.file );
93 lock.getFile().delete();
94 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
95 StandardCopyOption.COPY_ATTRIBUTES );
99 fileLockManager.release( lock );
101 logger.info( "thread1 ok" );
102 success.incrementAndGet();
105 public void thread2()
106 throws FileLockException, FileLockTimeoutException, IOException
108 logger.info( "thread2" );
109 Lock lock = fileLockManager.writeFileLock( this.file );
112 lock.getFile().delete();
113 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
114 StandardCopyOption.COPY_ATTRIBUTES );
118 fileLockManager.release( lock );
120 logger.info( "thread2 ok" );
121 success.incrementAndGet();
124 public void thread3()
125 throws FileLockException, FileLockTimeoutException, IOException
127 logger.info( "thread3" );
128 Lock lock = fileLockManager.readFileLock( this.file );
131 Files.copy( Paths.get( lock.getFile().getPath() ),
132 new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
136 fileLockManager.release( lock );
138 logger.info( "thread3 ok" );
139 success.incrementAndGet();
142 public void thread4()
143 throws FileLockException, FileLockTimeoutException, IOException
145 logger.info( "thread4" );
146 Lock lock = fileLockManager.writeFileLock( this.file );
149 lock.getFile().delete();
150 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
151 StandardCopyOption.COPY_ATTRIBUTES );
155 fileLockManager.release( lock );
157 logger.info( "thread4 ok" );
158 success.incrementAndGet();
161 public void thread5()
162 throws FileLockException, FileLockTimeoutException, IOException
164 logger.info( "thread5" );
165 Lock lock = fileLockManager.writeFileLock( this.file );
168 lock.getFile().delete();
169 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
170 StandardCopyOption.COPY_ATTRIBUTES );
174 fileLockManager.release( lock );
176 logger.info( "thread5 ok" );
177 success.incrementAndGet();
180 public void thread6()
181 throws FileLockException, FileLockTimeoutException, IOException
183 logger.info( "thread6" );
184 Lock lock = fileLockManager.readFileLock( this.file );
187 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
191 fileLockManager.release( lock );
193 logger.info( "thread6 ok" );
194 success.incrementAndGet();
197 public void thread7()
198 throws FileLockException, FileLockTimeoutException, IOException
200 logger.info( "thread7" );
201 Lock lock = fileLockManager.writeFileLock( this.file );
204 lock.getFile().delete();
205 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
206 StandardCopyOption.COPY_ATTRIBUTES );
210 fileLockManager.release( lock );
212 logger.info( "thread7 ok" );
213 success.incrementAndGet();
216 public void thread8()
217 throws FileLockException, FileLockTimeoutException, IOException
219 logger.info( "thread8" );
220 Lock lock = fileLockManager.readFileLock( this.file );
223 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
227 fileLockManager.release( lock );
229 logger.info( "thread8 ok" );
230 success.incrementAndGet();
233 public void thread9()
234 throws FileLockException, FileLockTimeoutException, IOException
236 logger.info( "thread7" );
237 Lock lock = fileLockManager.writeFileLock( this.file );
240 lock.getFile().delete();
241 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
242 StandardCopyOption.COPY_ATTRIBUTES );
246 fileLockManager.release( lock );
248 logger.info( "thread9 ok" );
249 success.incrementAndGet();
252 public void thread10()
253 throws FileLockException, FileLockTimeoutException, IOException
255 logger.info( "thread10" );
256 Lock lock = fileLockManager.readFileLock( this.file );
259 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
263 fileLockManager.release( lock );
265 logger.info( "thread8 ok" );
266 success.incrementAndGet();
274 public void initialize()
276 fileLockManager.setSkipLocking( false );
277 fileLockManager.clearLockFiles();
281 public void testWrite()
284 ConcurrentFileWrite concurrentFileWrite = new ConcurrentFileWrite( fileLockManager );
285 //concurrentFileWrite.setTrace( true );
286 TestFramework.runOnce( concurrentFileWrite );
287 logger.info( "success: {}", concurrentFileWrite.success );
288 Assert.assertEquals( 10, concurrentFileWrite.success.intValue() );