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