You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2012/10/25 09:48:13 UTC

svn commit: r1402001 - /db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/ExceptionMapperTest.java

Author: tfischer
Date: Thu Oct 25 07:48:13 2012
New Revision: 1402001

URL: http://svn.apache.org/viewvc?rev=1402001&view=rev
Log:
Make sure no timeout occurs in test

Modified:
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/ExceptionMapperTest.java

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/ExceptionMapperTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/ExceptionMapperTest.java?rev=1402001&r1=1402000&r2=1402001&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/ExceptionMapperTest.java (original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/ExceptionMapperTest.java Thu Oct 25 07:48:13 2012
@@ -36,7 +36,6 @@ import org.apache.torque.criteria.Criter
 import org.apache.torque.test.dbobject.Author;
 import org.apache.torque.test.dbobject.Book;
 import org.apache.torque.test.dbobject.SingleNamedUnique;
-import org.apache.torque.test.peer.AuthorPeer;
 import org.apache.torque.test.peer.SingleNamedUniquePeer;
 
 public class ExceptionMapperTest extends BaseDatabaseTestCase
@@ -45,7 +44,7 @@ public class ExceptionMapperTest extends
     private static int SLEEP_TIME = 10;
 
     /** Timeout for waiting for started threads and saves, in miliseconds. */
-    private static int TIMEOUT = 1000;
+    private static int TIMEOUT = 5000;
 
     private static Log log = LogFactory.getLog(ExceptionMapperTest.class);
 
@@ -197,8 +196,8 @@ public class ExceptionMapperTest extends
 
             // lock author 2 in transaction 1 (must wait for lock)
             author2.setName("newer Author2");
-            SaveThread saveThreadTransaction1
-                = new SaveThread(author2, transaction1);
+            SaveAndRollbackThread saveThreadTransaction1
+                = new SaveAndRollbackThread(author2, transaction1);
             saveThreadTransaction1.start();
 
             long startTime = System.currentTimeMillis();
@@ -213,14 +212,18 @@ public class ExceptionMapperTest extends
 
             // Try to lock author1 in transaction 2 (deadlock)
             author1.setName("newer Author1");
-            DeadlockException deadlockExceptionTransaction2 = null;
-            try
-            {
-                author1.save(transaction2);
-            }
-            catch (DeadlockException e)
+            SaveAndRollbackThread saveThreadTransaction2
+                = new SaveAndRollbackThread(author1, transaction2);
+            saveThreadTransaction2.start();
+
+            startTime = System.currentTimeMillis();
+            while (!author1.isSaving() && author1.isModified())
             {
-                deadlockExceptionTransaction2 = e;
+                Thread.sleep(SLEEP_TIME);
+                if (System.currentTimeMillis() > startTime + TIMEOUT)
+                {
+                    fail("Waited too long for saving to start (2)");
+                }
             }
 
             // wait till save on transaction 1 has finished
@@ -234,20 +237,35 @@ public class ExceptionMapperTest extends
                 }
             }
 
+            // wait till save on transaction 2 has finished
+            startTime = System.currentTimeMillis();
+            while (saveThreadTransaction2.isAlive())
+            {
+                Thread.sleep(10);
+                if (System.currentTimeMillis() > startTime + TIMEOUT)
+                {
+                    fail("Waited too long for saving to finish (2)");
+                }
+            }
+
             // verify. Either in transaction 1 or in transaction 2
             // a deadlock exception must have occurred
-            if (deadlockExceptionTransaction2 != null)
+            if (saveThreadTransaction1.getCaughtException() != null)
             {
-                return;
+                if (!(saveThreadTransaction1.getCaughtException()
+                        instanceof DeadlockException))
+                {
+                    throw saveThreadTransaction1.getCaughtException();
+                }
             }
-            if (saveThreadTransaction1.getCaughtException() == null)
+            else if (saveThreadTransaction2.getCaughtException() == null)
             {
                 fail("No Deadlock occured");
-            }
-            if (!(saveThreadTransaction1.getCaughtException()
-                    instanceof DeadlockException))
-            {
-                throw saveThreadTransaction1.getCaughtException();
+                if (!(saveThreadTransaction2.getCaughtException()
+                        instanceof DeadlockException))
+                {
+                    throw saveThreadTransaction2.getCaughtException();
+                }
             }
         }
         finally
@@ -257,7 +275,7 @@ public class ExceptionMapperTest extends
         }
     }
 
-    private static class SaveThread extends Thread
+    private static class SaveAndRollbackThread extends Thread
     {
         private final Author toSave;
 
@@ -265,7 +283,7 @@ public class ExceptionMapperTest extends
 
         private TorqueException caughtException;
 
-        public SaveThread(Author toSave, Connection transaction)
+        public SaveAndRollbackThread(Author toSave, Connection transaction)
         {
             this.toSave = toSave;
             this.transaction = transaction;
@@ -282,6 +300,10 @@ public class ExceptionMapperTest extends
             {
                 caughtException = e;
             }
+            finally
+            {
+                Transaction.safeRollback(transaction);
+            }
         }
 
         public TorqueException getCaughtException()



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org