[jbosscache-commits] JBoss Cache SVN: r4462 - in core/trunk/src/test/java/org/jboss/cache: lock and 2 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Sep 14 11:51:46 EDT 2007


Author: manik.surtani at jboss.com
Date: 2007-09-14 11:51:46 -0400 (Fri, 14 Sep 2007)
New Revision: 4462

Added:
   core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/
   core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/ConcurrentPutRemoveTest.java
   core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockNotReleasedTest.java
   core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockUpgradeTest.java
   core/trunk/src/test/java/org/jboss/cache/transaction/AsyncRollbackTxLockTest.java
Removed:
   core/trunk/src/test/java/org/jboss/cache/ConcurrentPutRemoveTest.java
Log:
Added tests for a series of bugs, including JBCACHE-923, JBCACHE-1164, JBCACHE-1165, JBCACHE-1166, JBCACHE-1168, JBCACHE-1183.

Deleted: core/trunk/src/test/java/org/jboss/cache/ConcurrentPutRemoveTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/ConcurrentPutRemoveTest.java	2007-09-14 15:50:44 UTC (rev 4461)
+++ core/trunk/src/test/java/org/jboss/cache/ConcurrentPutRemoveTest.java	2007-09-14 15:51:46 UTC (rev 4462)
@@ -1,87 +0,0 @@
-package org.jboss.cache;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.transaction.TransactionManager;
-
-import junit.framework.TestCase;
-
-import org.jboss.cache.lock.IsolationLevel;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.transaction.DummyTransactionManagerLookup;
-
-public class ConcurrentPutRemoveTest extends TestCase {
-
-	private TransactionManager tm;
-
-	private Cache cache;
-
-	protected void setUp() throws Exception {
-		cache = DefaultCacheFactory.getInstance().createCache(false);
-		cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
-		cache.getConfiguration().setIsolationLevel(IsolationLevel.READ_COMMITTED);
-		cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
-		cache.getConfiguration().setLockAcquisitionTimeout(10000);
-		cache.start();
-		tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
-	}
-
-	protected void tearDown() throws Exception {
-		super.tearDown();
-		cache.stop();
-		cache.destroy();
-	}
-
-	public void testLock() throws Exception {
-		List<SeparateThread> threads = new ArrayList<SeparateThread>();
-		for (int x = 0; x < 2; x++) {
-			SeparateThread t = new SeparateThread(x);
-			threads.add(t);
-			t.start();
-		}
-		for (SeparateThread separateThread : threads) {
-			separateThread.join();
-			if (separateThread.getException() != null) {
-				throw separateThread.getException();
-			}
-		}
-
-	}
-
-	private class SeparateThread extends Thread {
-		Exception e = null;
-
-		private int num = 0;
-
-		public SeparateThread(int num) {
-			this.num = num;
-		}
-
-		public Exception getException() {
-			return e;
-		}
-
-		public void run() {
-
-         Thread.currentThread().setName("Thread:" + num);
-			try {
-				for (int x = 0; x < 1000; x++) {
-					tm.begin();
-					System.out.println("R" + Thread.currentThread().getName());
-					//inside transaction
-					cache.removeNode(Fqn.fromString("/a"));
-					System.out.println("AR" + Thread.currentThread().getName());
-					tm.commit();
-					//outside transaction
-					System.out.println("P" + Thread.currentThread().getName());
-					cache.put(Fqn.fromString("/a/b/c/d"), "text"+x,"b");
-					System.out.println("AP" + Thread.currentThread().getName());
-				}
-			} catch (Exception e) {
-				this.e = e;
-			}
-		}
-	}
-
-}

Added: core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/ConcurrentPutRemoveTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/ConcurrentPutRemoveTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/ConcurrentPutRemoveTest.java	2007-09-14 15:51:46 UTC (rev 4462)
@@ -0,0 +1,92 @@
+package org.jboss.cache.lock.pessimistic;
+
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.AfterMethod;
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.lock.IsolationLevel;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.transaction.DummyTransactionManagerLookup;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.TransactionManager;
+
+ at Test(groups = {"functional"})
+public class ConcurrentPutRemoveTest
+{
+	private TransactionManager tm;
+
+	private Cache cache;
+
+   @BeforeMethod(alwaysRun = true)
+   public void setUp() throws Exception {
+		cache = DefaultCacheFactory.getInstance().createCache(false);
+      cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
+		cache.getConfiguration().setIsolationLevel(IsolationLevel.READ_COMMITTED);
+		cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+		cache.getConfiguration().setLockAcquisitionTimeout(10000);
+		cache.start();
+		tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
+	}
+
+   @AfterMethod(alwaysRun = true)
+   public void tearDown() throws Exception
+   {
+		cache.destroy();
+	}
+
+   public void testLock() throws Exception {
+		List<SeparateThread> threads = new ArrayList<SeparateThread>();
+		for (int x = 0; x < 2; x++) {
+			SeparateThread t = new SeparateThread(x);
+			threads.add(t);
+			t.start();
+		}
+		for (SeparateThread separateThread : threads) {
+			separateThread.join();
+			if (separateThread.getException() != null) {
+				throw separateThread.getException();
+			}
+		}
+
+	}
+
+	private class SeparateThread extends Thread {
+		Exception e = null;
+
+		private int num = 0;
+
+		public SeparateThread(int num) {
+			this.num = num;
+		}
+
+		public Exception getException() {
+			return e;
+		}
+
+		public void run() {
+			Thread.currentThread().setName("Thread:" + num);
+			try {
+				for (int x = 0; x < 1000; x++) {
+					tm.begin();
+					System.out.println("R" + Thread.currentThread().getName());
+					//inside transaction
+					cache.removeNode(Fqn.fromString("/a"));
+					System.out.println("AR" + Thread.currentThread().getName());
+					tm.commit();
+					//outside transaction
+					System.out.println("P" + Thread.currentThread().getName());
+					cache.put(Fqn.fromString("/a/b/c/d"), "text"+x,"b");
+					System.out.println("AP" + Thread.currentThread().getName());
+				}
+			} catch (Exception e) {
+				this.e = e;
+			}
+		}
+	}
+
+}

Added: core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockNotReleasedTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockNotReleasedTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockNotReleasedTest.java	2007-09-14 15:51:46 UTC (rev 4462)
@@ -0,0 +1,161 @@
+package org.jboss.cache.lock.pessimistic;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import javax.transaction.TransactionManager;
+
+
+import org.jboss.cache.CacheException;
+import org.jboss.cache.Node;
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.UnversionedNode;
+import org.jboss.cache.CacheImpl;
+import org.jboss.cache.config.ConfigurationComponent;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.transaction.DummyTransactionManagerLookup;
+import org.jboss.cache.lock.IsolationLevel;
+import org.testng.annotations.Test;
+import org.testng.annotations.AfterMethod;
+
+ at Test(groups = {"functional"})
+public class LockNotReleasedTest
+{
+	Random rand = new Random(System.currentTimeMillis());
+
+	private TransactionManager tm;
+
+	private Cache cache;
+
+	private void startTest() throws Exception {
+		cache = DefaultCacheFactory.getInstance().createCache(false);
+		cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
+		cache.getConfiguration().setIsolationLevel(IsolationLevel.READ_COMMITTED);
+		cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+		cache.getConfiguration().setLockAcquisitionTimeout(1000);
+		cache.create();
+		cache.start();
+		tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
+	}
+
+   @AfterMethod(alwaysRun = true)
+   public void tearDown() throws Exception {
+		cache.stop();
+		cache.destroy();
+	}
+
+
+	   public int getNumberOfLocksHeld()
+	   {
+	      return numLocks(cache.getRoot());
+	   }
+
+	   private int numLocks(Node node)
+	   {
+         UnversionedNode n = (UnversionedNode) node;
+         int num = 0;
+	      Map children;
+
+         if (n.getLock().isLocked()){
+	         num++;
+	         System.out.println("Node ["+n.getFqn()+"] is locked");
+	      }
+	      if ((children = n.getChildrenMapDirect()) != null)
+	      {
+            for (Object o : children.values())
+            {
+               num += numLocks((Node) o);
+            }
+         }
+	      return num;
+	   }
+
+	public void testTransactionStorm() throws Exception {
+		startTest();
+		List<WorkThread> threads = new ArrayList<WorkThread>();
+//		System.out.println(cache.getNumberOfLocksHeld());
+		assert 0 == getNumberOfLocksHeld();
+		while (true) {
+			for (int x = 0; x < 2; x++) {
+				WorkThread t = new WorkThread(x == 1);
+				threads.add(t);
+				t.start();
+			}
+			for (WorkThread separateThread : threads) {
+				separateThread.join();
+				if (separateThread.getException() != null) {
+					//separateThread.getException().getMessage();
+				}
+			}
+			int locksNum = getNumberOfLocksHeld();
+//			System.out.println("Locks="+locksNum);
+			// checkpoint
+			if (((CacheImpl) cache).getNumberOfLocksHeld()>0){
+				System.out.println("ERROR, locks="+locksNum);
+				doDomethingOnCache(1);
+			}
+			assert 0 == locksNum;
+		}
+
+	}
+
+	private class WorkThread extends Thread {
+		Exception e = null;
+
+		private boolean remove;
+
+		public WorkThread(boolean remove) {
+			this.remove = remove;
+		}
+
+		public Exception getException() {
+			return e;
+		}
+
+		public void run() {
+			try {
+				for (int x = 0; x < 100; x++) {
+					tm.begin();
+					try {
+							doDomethingOnCache(x);
+					} finally {
+						if (x % 3 == 0) {
+							tm.commit();
+						} else {
+							tm.rollback();
+						}
+					}
+				}
+			} catch (Exception e) {
+//				System.out.println(e.getMessage());
+				this.e = e;
+			}
+		}
+
+	}
+
+	private void doDomethingOnCache(int x) throws CacheException, InterruptedException {
+		cache.put(Fqn.fromString("/a/b/c/a"), "text1" + x, "");
+		cache.removeNode(Fqn.fromString("/q/b/c/d"));
+		cache.put(Fqn.fromString("/a/b/c/b"), "text2" + x, "");
+		cache.removeNode(Fqn.fromString("/q/b/c/c"));
+		cache.put(Fqn.fromString("/a/b/c/c"), "text3" + x, "");
+		cache.removeNode(Fqn.fromString("/q/b/c/b"));
+		cache.put(Fqn.fromString("/a/b/c/d"), "text4" + x, "");
+		cache.removeNode(Fqn.fromString("/q/b/c/a"));
+		cache.put(Fqn.fromString("/q/b/c/a"), "text5" + x, "");
+		cache.removeNode(Fqn.fromString("/a/b/c/d"));
+		cache.put(Fqn.fromString("/q/b/c/b"), "text6" + x, "");
+		cache.removeNode(Fqn.fromString("/a/b/c/c"));
+		cache.put(Fqn.fromString("/q/b/c/c"), "text7" + x, "");
+		cache.removeNode(Fqn.fromString("/a/b/c/b"));
+		cache.put(Fqn.fromString("/q/b/c/d"), "text8" + x, "");
+		cache.removeNode(Fqn.fromString("/a/b/c/a"));
+	}
+
+}

Added: core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockUpgradeTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockUpgradeTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/pessimistic/LockUpgradeTest.java	2007-09-14 15:51:46 UTC (rev 4462)
@@ -0,0 +1,93 @@
+package org.jboss.cache.lock.pessimistic;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.TransactionManager;
+
+import junit.framework.TestCase;
+
+import org.jboss.cache.CacheImpl;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.jboss.cache.transaction.DummyTransactionManagerLookup;
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.AfterMethod;
+
+/**
+ * Test behaviour of concurent put/remove
+ *
+ * @author jhalat
+ */
+ at Test(groups = {"functional"})
+public class LockUpgradeTest{
+
+	private TransactionManager tm;
+
+	private CacheImpl cache;
+
+   @BeforeMethod(alwaysRun = true)
+   protected void setUp() throws Exception {
+		Configuration c = new Configuration();
+		c.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+      cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
+		tm = DummyTransactionManager.getInstance();
+	}
+
+   @AfterMethod(alwaysRun = true)
+   protected void tearDown() throws Exception {
+		cache.stop();
+		cache.destroy();
+	}
+
+	public void testLock() throws Exception {
+		List<WorkThread> threads = new ArrayList<WorkThread>();
+		for (int x = 0; x < 2; x++) {
+			WorkThread t = new WorkThread(x == 1);
+			threads.add(t);
+			t.start();
+		}
+		for (WorkThread separateThread : threads) {
+			separateThread.join();
+			if (separateThread.getException() != null) {
+				throw separateThread.getException();
+			}
+		}
+	}
+
+	private class WorkThread extends Thread {
+		Exception e = null;
+
+		private boolean remove;
+
+		public WorkThread(boolean remove) {
+			this.remove = remove;
+		}
+
+		public Exception getException() {
+			return e;
+		}
+
+		public void run() {
+			try {
+				for (int x = 0; x < 1000; x++) {
+					tm.begin();
+					if (remove) {
+						cache.remove("/a/b/c/d");
+						cache.remove("/a");
+					} else {
+						cache.put("/a/b/c/d", "key","text" + x);
+					}
+					tm.commit();
+					Thread.sleep(1);
+				}
+			} catch (Exception e) {
+				this.e = e;
+			}
+		}
+	}
+}

Added: core/trunk/src/test/java/org/jboss/cache/transaction/AsyncRollbackTxLockTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/transaction/AsyncRollbackTxLockTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/transaction/AsyncRollbackTxLockTest.java	2007-09-14 15:51:46 UTC (rev 4462)
@@ -0,0 +1,98 @@
+package org.jboss.cache.transaction;
+
+import junit.framework.TestCase;
+
+
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.CacheImpl;
+import org.jboss.cache.Fqn;
+import org.testng.annotations.Test;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+
+/**
+ * Test behaviour of async rollback timeouted transaction
+ *
+ * @author <a href="mailto:jhalat at infovide.pl">Jacek Halat</a>
+ */
+ at Test(groups = {"functional"})
+public class AsyncRollbackTxLockTest
+{
+   private Cache cache;
+   private TransactionManager tm;
+
+
+   @BeforeMethod(alwaysRun = true)
+   public void setUp() throws Exception
+   {
+      cache = DefaultCacheFactory.getInstance().createCache(false);
+
+      cache.getConfiguration().setTransactionManagerLookupClass(AsyncRollbackTransactionManagerLookup.class.getName());
+      cache.start();
+      tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
+      tm.setTransactionTimeout(2);
+   }
+
+   @AfterMethod(alwaysRun = true)
+   public void tearDown()
+   {
+      try
+      {
+         if (tm != null && tm.getTransaction() != null)
+         {
+            try
+            {
+               tm.rollback();
+            }
+            catch (SystemException e)
+            {
+               // do nothing
+            }
+         }
+      }
+      catch (SystemException e)
+      {
+         // do nothing
+      }
+      if (cache != null) cache.stop();
+      cache = null;
+      tm = null;
+   }
+
+
+   public void testTxTimeoutAndRemovePutAfter() throws Exception
+   {
+	   Thread.currentThread().setName("Thread-0");
+		  System.out.println("Main Thread:"+Thread.currentThread());
+	      assert 0 == ((CacheImpl) cache).getNumberOfLocksHeld();
+	      tm.setTransactionTimeout(1);//short transaction timeout
+	      cache.put(Fqn.fromString("/a/b/c/d"), "k", "v");
+	      tm.begin();
+	      Transaction transaction = tm.getTransaction();
+	      transaction.registerSynchronization(new Synchronization(){
+
+			public void afterCompletion(int arg0) {
+				System.out.println("Synchronization Thread:"+Thread.currentThread());
+			}
+
+			public void beforeCompletion() {
+			}
+
+	      });
+	      assert tm.getTransaction() != null;
+	      Thread.sleep(500);//transaction should be rolledback in another thread
+	      cache.put(Fqn.fromString("/a"), "k", "v");
+	      tm.rollback();
+	      assert tm.getTransaction() == null;
+	      assert 0 == ((CacheImpl) cache).getNumberOfLocksHeld();
+	      cache.put(Fqn.fromString("/a"), "k", "v");
+   }
+
+}
+




More information about the jbosscache-commits mailing list