From 367997a308c7405f340c244bee2f8fa898f77c95 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Thu, 27 Oct 2016 17:43:31 +0200 Subject: [PATCH] Add additional checks for file lock tests --- .../filelock/DefaultFileLockManagerTest.java | 349 ++++++++++-------- 1 file changed, 186 insertions(+), 163 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java index 19f836dbc..7fa30b566 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java @@ -33,6 +33,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.inject.Inject; import javax.inject.Named; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; @@ -48,53 +49,49 @@ import java.util.concurrent.atomic.AtomicInteger; * @author Olivier Lamy */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" }) -public class DefaultFileLockManagerTest -{ +@ContextConfiguration(locations = {"classpath*:/META-INF/spring-context.xml"}) +public class DefaultFileLockManagerTest { - final Logger logger = LoggerFactory.getLogger( getClass() ); + final Logger logger = LoggerFactory.getLogger(getClass()); @Inject @Named(value = "fileLockManager#default") FileLockManager fileLockManager; class ConcurrentFileWrite - extends MultithreadedTestCase - { + extends MultithreadedTestCase { - AtomicInteger success = new AtomicInteger( 0 ); + AtomicInteger success = new AtomicInteger(0); FileLockManager fileLockManager; - File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" ); + File file = new File(System.getProperty("buildDirectory"), "foo.txt"); - File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" ); + File largeJar = new File(System.getProperty("basedir"), "src/test/cassandra-all-2.0.3.jar"); - ConcurrentFileWrite( FileLockManager fileLockManager ) - throws IOException - { + ConcurrentFileWrite(FileLockManager fileLockManager) + throws IOException { this.fileLockManager = fileLockManager; //file.createNewFile(); } @Override - public void initialize() - { + public void initialize() { } // Files.copy is not atomic so have to try several times in // a multithreaded test private void copyFile(Path source, Path destination) { - int attempts=10; + int attempts = 10; boolean finished = false; - while(!finished && attempts-->0) { + while (!finished && attempts-- > 0) { try { Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); - finished=true; + finished = true; } catch (IOException ex) { // } @@ -102,180 +99,208 @@ public class DefaultFileLockManagerTest } public void thread1() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread1" ); - Lock lock = fileLockManager.writeFileLock( this.file ); - try - { - lock.getFile().delete(); - copyFile( largeJar.toPath(), lock.getFile().toPath()); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread1"); + Lock lock = fileLockManager.writeFileLock(this.file); + try { + lock.getFile().delete(); + copyFile(largeJar.toPath(), lock.getFile().toPath()); + } finally { + fileLockManager.release(lock); + } + logger.info("thread1 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread1 ok" ); - success.incrementAndGet(); } public void thread2() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread2" ); - Lock lock = fileLockManager.writeFileLock( this.file ); - try - { - lock.getFile().delete(); - copyFile( largeJar.toPath(), lock.getFile().toPath()); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread2"); + Lock lock = fileLockManager.writeFileLock(this.file); + try { + lock.getFile().delete(); + copyFile(largeJar.toPath(), lock.getFile().toPath()); + } finally { + fileLockManager.release(lock); + } + logger.info("thread2 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread2 ok" ); - success.incrementAndGet(); + } public void thread3() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread3" ); - Lock lock = fileLockManager.readFileLock( this.file ); - try - { - Files.copy( Paths.get( lock.getFile().getPath() ), - new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread3"); + Lock lock = fileLockManager.readFileLock(this.file); + try { + Files.copy(Paths.get(lock.getFile().getPath()), + new FileOutputStream(File.createTempFile("foo", ".jar"))); + } finally { + fileLockManager.release(lock); + } + logger.info("thread3 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread3 ok" ); - success.incrementAndGet(); + } public void thread4() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread4" ); - Lock lock = fileLockManager.writeFileLock( this.file ); - try - { - lock.getFile().delete(); - copyFile( largeJar.toPath(), lock.getFile().toPath()); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread4"); + Lock lock = fileLockManager.writeFileLock(this.file); + try { + lock.getFile().delete(); + copyFile(largeJar.toPath(), lock.getFile().toPath()); + } finally { + fileLockManager.release(lock); + } + logger.info("thread4 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread4 ok" ); - success.incrementAndGet(); + } public void thread5() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread5" ); - Lock lock = fileLockManager.writeFileLock( this.file ); - try - { - lock.getFile().delete(); - copyFile( largeJar.toPath(), lock.getFile().toPath()); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread5"); + Lock lock = fileLockManager.writeFileLock(this.file); + try { + lock.getFile().delete(); + copyFile(largeJar.toPath(), lock.getFile().toPath()); + } finally { + fileLockManager.release(lock); + } + logger.info("thread5 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread5 ok" ); - success.incrementAndGet(); + } public void thread6() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread6" ); - Lock lock = fileLockManager.readFileLock( this.file ); - try - { - Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread6"); + Lock lock = fileLockManager.readFileLock(this.file); + try { + Files.copy(lock.getFile().toPath(), new FileOutputStream(File.createTempFile("foo", ".jar"))); + } finally { + fileLockManager.release(lock); + } + logger.info("thread6 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread6 ok" ); - success.incrementAndGet(); + } public void thread7() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread7" ); - Lock lock = fileLockManager.writeFileLock( this.file ); - try - { - lock.getFile().delete(); - copyFile( largeJar.toPath(), lock.getFile().toPath()); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread7"); + Lock lock = fileLockManager.writeFileLock(this.file); + try { + lock.getFile().delete(); + copyFile(largeJar.toPath(), lock.getFile().toPath()); + } finally { + fileLockManager.release(lock); + } + logger.info("thread7 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread7 ok" ); - success.incrementAndGet(); + } public void thread8() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread8" ); - Lock lock = fileLockManager.readFileLock( this.file ); - try - { - Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread8"); + Lock lock = fileLockManager.readFileLock(this.file); + try { + Files.copy(lock.getFile().toPath(), new FileOutputStream(File.createTempFile("foo", ".jar"))); + } finally { + fileLockManager.release(lock); + } + logger.info("thread8 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread8 ok" ); - success.incrementAndGet(); + } public void thread9() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread9" ); - Lock lock = fileLockManager.writeFileLock( this.file ); - try - { - lock.getFile().delete(); - copyFile( largeJar.toPath(), lock.getFile().toPath()); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread9"); + Lock lock = fileLockManager.writeFileLock(this.file); + try { + lock.getFile().delete(); + copyFile(largeJar.toPath(), lock.getFile().toPath()); + } finally { + fileLockManager.release(lock); + } + logger.info("thread9 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread9 ok" ); - success.incrementAndGet(); } public void thread10() - throws FileLockException, FileLockTimeoutException, IOException - { - logger.info( "thread10" ); - Lock lock = fileLockManager.readFileLock( this.file ); - try - { - Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) ); - } - finally - { - fileLockManager.release( lock ); + throws FileLockException, FileLockTimeoutException, IOException { + try { + logger.info("thread10"); + Lock lock = fileLockManager.readFileLock(this.file); + try { + Files.copy(lock.getFile().toPath(), new FileOutputStream(File.createTempFile("foo", ".jar"))); + } finally { + fileLockManager.release(lock); + } + logger.info("thread10 ok"); + success.incrementAndGet(); + } catch (Throwable e) { + logger.error("Error occured " + e.getMessage()); + e.printStackTrace(); + throw e; } - logger.info( "thread10 ok" ); - success.incrementAndGet(); + } @@ -283,21 +308,19 @@ public class DefaultFileLockManagerTest @Before - public void initialize() - { - fileLockManager.setSkipLocking( false ); + public void initialize() { + fileLockManager.setSkipLocking(false); fileLockManager.clearLockFiles(); } @Test public void testWrite() - throws Throwable - { - ConcurrentFileWrite concurrentFileWrite = new ConcurrentFileWrite( fileLockManager ); + throws Throwable { + ConcurrentFileWrite concurrentFileWrite = new ConcurrentFileWrite(fileLockManager); //concurrentFileWrite.setTrace( true ); - TestFramework.runManyTimes( concurrentFileWrite, 10); - logger.info( "success: {}", concurrentFileWrite.success ); - Assert.assertEquals( 100, concurrentFileWrite.success.intValue() ); + TestFramework.runManyTimes(concurrentFileWrite, 10, TestFramework.DEFAULT_CLOCKPERIOD, 20); + logger.info("success: {}", concurrentFileWrite.success); + Assert.assertEquals(100, concurrentFileWrite.success.intValue()); } -- 2.39.5