You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ma...@apache.org on 2017/06/10 11:25:58 UTC

[13/50] archiva git commit: Add additional checks for file lock tests

Add additional checks for file lock tests


Project: http://git-wip-us.apache.org/repos/asf/archiva/repo
Commit: http://git-wip-us.apache.org/repos/asf/archiva/commit/dfcef53b
Tree: http://git-wip-us.apache.org/repos/asf/archiva/tree/dfcef53b
Diff: http://git-wip-us.apache.org/repos/asf/archiva/diff/dfcef53b

Branch: refs/heads/citest
Commit: dfcef53bf9d32839e280ac46ccb3397d2ce0ba05
Parents: 4c35cce
Author: Martin Stockhammer <ma...@apache.org>
Authored: Thu Oct 27 17:43:31 2016 +0200
Committer: Martin Stockhammer <ma...@apache.org>
Committed: Thu Oct 27 23:10:49 2016 +0200

----------------------------------------------------------------------
 .../filelock/DefaultFileLockManagerTest.java    | 349 ++++++++++---------
 1 file changed, 186 insertions(+), 163 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/archiva/blob/dfcef53b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java
----------------------------------------------------------------------
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 19f836d..7fa30b5 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());
     }