]> source.dussan.org Git - archiva.git/blob
cc63e0c3afcb164bd378dc9046975562b7feb381
[archiva.git] /
1 package org.apache.archiva.common.filelock;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
32
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import java.io.File;
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;
42
43 //import org.apache.commons.io.IOUtils;
44
45 /**
46  * @author Olivier Lamy
47  */
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
50 public class DefaultFileLockManagerTest
51 {
52
53     final Logger logger = LoggerFactory.getLogger( getClass() );
54
55     @Inject
56     @Named(value = "fileLockManager#default")
57     FileLockManager fileLockManager;
58
59     class ConcurrentFileWrite
60         extends MultithreadedTestCase
61     {
62
63
64         AtomicInteger success = new AtomicInteger( 0 );
65
66         FileLockManager fileLockManager;
67
68         File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" );
69
70         File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" );
71
72         ConcurrentFileWrite( FileLockManager fileLockManager )
73             throws IOException
74         {
75             this.fileLockManager = fileLockManager;
76             //file.createNewFile();
77
78         }
79
80         @Override
81         public void initialize()
82         {
83
84         }
85
86         public void thread1()
87             throws FileLockException, FileLockTimeoutException, IOException
88         {
89             logger.info( "thread1" );
90             Lock lock = fileLockManager.writeFileLock( this.file );
91             try
92             {
93                 lock.getFile().delete();
94                 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
95                             StandardCopyOption.COPY_ATTRIBUTES );
96             }
97             finally
98             {
99                 fileLockManager.release( lock );
100             }
101             logger.info( "thread1 ok" );
102             success.incrementAndGet();
103         }
104
105         public void thread2()
106             throws FileLockException, FileLockTimeoutException, IOException
107         {
108             logger.info( "thread2" );
109             Lock lock = fileLockManager.writeFileLock( this.file );
110             try
111             {
112                 lock.getFile().delete();
113                 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
114                             StandardCopyOption.COPY_ATTRIBUTES );
115             }
116             finally
117             {
118                 fileLockManager.release( lock );
119             }
120             logger.info( "thread2 ok" );
121             success.incrementAndGet();
122         }
123
124         public void thread3()
125             throws FileLockException, FileLockTimeoutException, IOException
126         {
127             logger.info( "thread3" );
128             Lock lock = fileLockManager.readFileLock( this.file );
129             try
130             {
131                 Files.copy( Paths.get( lock.getFile().getPath() ),
132                             new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
133             }
134             finally
135             {
136                 fileLockManager.release( lock );
137             }
138             logger.info( "thread3 ok" );
139             success.incrementAndGet();
140         }
141
142         public void thread4()
143             throws FileLockException, FileLockTimeoutException, IOException
144         {
145             logger.info( "thread4" );
146             Lock lock = fileLockManager.writeFileLock( this.file );
147             try
148             {
149                 lock.getFile().delete();
150                 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
151                             StandardCopyOption.COPY_ATTRIBUTES );
152             }
153             finally
154             {
155                 fileLockManager.release( lock );
156             }
157             logger.info( "thread4 ok" );
158             success.incrementAndGet();
159         }
160
161         public void thread5()
162             throws FileLockException, FileLockTimeoutException, IOException
163         {
164             logger.info( "thread5" );
165             Lock lock = fileLockManager.writeFileLock( this.file );
166             try
167             {
168                 lock.getFile().delete();
169                 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
170                             StandardCopyOption.COPY_ATTRIBUTES );
171             }
172             finally
173             {
174                 fileLockManager.release( lock );
175             }
176             logger.info( "thread5 ok" );
177             success.incrementAndGet();
178         }
179
180         public void thread6()
181             throws FileLockException, FileLockTimeoutException, IOException
182         {
183             logger.info( "thread6" );
184             Lock lock = fileLockManager.readFileLock( this.file );
185             try
186             {
187                 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
188             }
189             finally
190             {
191                 fileLockManager.release( lock );
192             }
193             logger.info( "thread6 ok" );
194             success.incrementAndGet();
195         }
196
197         public void thread7()
198             throws FileLockException, FileLockTimeoutException, IOException
199         {
200             logger.info( "thread7" );
201             Lock lock = fileLockManager.writeFileLock( this.file );
202             try
203             {
204                 lock.getFile().delete();
205                 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
206                             StandardCopyOption.COPY_ATTRIBUTES );
207             }
208             finally
209             {
210                 fileLockManager.release( lock );
211             }
212             logger.info( "thread7 ok" );
213             success.incrementAndGet();
214         }
215
216         public void thread8()
217             throws FileLockException, FileLockTimeoutException, IOException
218         {
219             logger.info( "thread8" );
220             Lock lock = fileLockManager.readFileLock( this.file );
221             try
222             {
223                 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
224             }
225             finally
226             {
227                 fileLockManager.release( lock );
228             }
229             logger.info( "thread8 ok" );
230             success.incrementAndGet();
231         }
232
233         public void thread9()
234             throws FileLockException, FileLockTimeoutException, IOException
235         {
236             logger.info( "thread7" );
237             Lock lock = fileLockManager.writeFileLock( this.file );
238             try
239             {
240                 lock.getFile().delete();
241                 Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING,
242                             StandardCopyOption.COPY_ATTRIBUTES );
243             }
244             finally
245             {
246                 fileLockManager.release( lock );
247             }
248             logger.info( "thread9 ok" );
249             success.incrementAndGet();
250         }
251
252         public void thread10()
253             throws FileLockException, FileLockTimeoutException, IOException
254         {
255             logger.info( "thread10" );
256             Lock lock = fileLockManager.readFileLock( this.file );
257             try
258             {
259                 Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
260             }
261             finally
262             {
263                 fileLockManager.release( lock );
264             }
265             logger.info( "thread8 ok" );
266             success.incrementAndGet();
267         }
268
269
270     }
271
272
273     @Before
274     public void initialize()
275     {
276         fileLockManager.setSkipLocking( false );
277         fileLockManager.clearLockFiles();
278     }
279
280     @Test
281     public void testWrite()
282         throws Throwable
283     {
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() );
289     }
290
291
292 }