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