[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/transaction ...
Manik Surtani
msurtani at jboss.com
Fri Nov 24 09:19:30 EST 2006
User: msurtani
Date: 06/11/24 09:19:30
Modified: tests/functional/org/jboss/cache/transaction Tag:
Branch_JBossCache_1_3_0 TransactionTest.java
Log:
Fixes to JBCACHE-875
Revision Changes Path
No revision
No revision
1.9.2.3 +134 -65 JBossCache/tests/functional/org/jboss/cache/transaction/TransactionTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: TransactionTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/transaction/TransactionTest.java,v
retrieving revision 1.9.2.2
retrieving revision 1.9.2.3
diff -u -b -r1.9.2.2 -r1.9.2.3
--- TransactionTest.java 23 Nov 2006 17:57:10 -0000 1.9.2.2
+++ TransactionTest.java 24 Nov 2006 14:19:30 -0000 1.9.2.3
@@ -27,7 +27,7 @@
* Tests transactional access to a local TreeCache.
* Note: we use DummpyTranasctionManager to replace jta
*
- * @version $Id: TransactionTest.java,v 1.9.2.2 2006/11/23 17:57:10 msurtani Exp $
+ * @version $Id: TransactionTest.java,v 1.9.2.3 2006/11/24 14:19:30 msurtani Exp $
*/
public class TransactionTest extends TestCase {
TreeCache cache=null;
@@ -316,7 +316,7 @@
cache.put("/a/b/c", null);
tx.begin();
gtx=cache.getCurrentTransaction();
- cache.remove("/a/b/c", null);
+ cache.remove("/a/b/c"); // need to remove the node, not just the data in the node.
assertLocked(gtx, "/a", false);
assertLocked(gtx, "/a/b", true);
assertLocked(gtx, "/a/b/c", true);
@@ -329,17 +329,15 @@
}
- public void testNodeDeletionRollback3() {
+ public void testNodeDeletionRollback3() throws Exception{
GlobalTransaction gtx;
- try {
-
cache.put("/a/b/c1", null);
tx.begin();
gtx=cache.getCurrentTransaction();
cache.put("/a/b/c1", null);
assertLocked(gtx, "/a", false);
- assertLocked(gtx, "/a/b", true);
+ assertLocked(gtx, "/a/b", false);
assertLocked(gtx, "/a/b/c1", true);
cache.put("/a/b/c2", null);
@@ -360,48 +358,53 @@
cache.put("/a/b/c1/two/2/3/4", null);
assertLocked(gtx, "/a/b/c1", true);
assertLocked(gtx, "/a/b/c1/two", true);
- assertLocked(gtx, "/a/b/c1/two/2", false);
- assertLocked(gtx, "/a/b/c1/two/2/3", false);
+ assertLocked(gtx, "/a/b/c1/two/2", true);
+ assertLocked(gtx, "/a/b/c1/two/2/3", true);
assertLocked(gtx, "/a/b/c1/two/2/3/4", true);
System.out.println("locks: " + cache.printLockInfo());
cache.remove("/a/b");
tx.rollback();
- assertNull(cache.getChildrenNames("/a/b"));
- }
- catch(Throwable t) {
- t.printStackTrace();
- fail(t.toString());
- }
+ assertTrue(cache.getChildrenNames("/a/b/c1").isEmpty());
+ Set cn = cache.getChildrenNames("/a/b");
+ assertEquals(1, cn.size());
+ assertEquals("c1", cn.iterator().next());
}
- public void testDoubleLocks() {
- try {
+ public void testDoubleLocks() throws Exception{
tx.begin();
+ GlobalTransaction gtx = cache.getCurrentTransaction();
cache.put("/a/b/c", null);
cache.put("/a/b/c", null);
DataNode n=cache.get("/a");
IdentityLock lock=n.getLock();
int num=lock.getReaderOwners().size();
- assertEquals(1, num);
+ assertEquals(0, num);
+ // make sure this is write locked.
+ assertLocked(gtx, "/a", true);
n=cache.get("/a/b");
lock=n.getLock();
num=lock.getReaderOwners().size();
- assertEquals(1, num);
- // added this here since we had stale gtxs lurking about. - Manik, 02Jan06
+ assertEquals(0, num);
+ // make sure this is write locked.
+ assertLocked(gtx, "/a/b", true);
+
+ n=cache.get("/a/b/c");
+ lock=n.getLock();
+ num=lock.getReaderOwners().size();
+ assertEquals(0, num);
+ // make sure this is write locked.
+ assertLocked(gtx, "/a/b/c", true);
+
tx.rollback();
- }
- catch(Throwable t) {
- t.printStackTrace();
- fail(t.toString());
- }
+ assertEquals(0, cache.getNumberOfLocksHeld());
}
private void assertLocked(Object owner, String fqn, boolean write_locked) throws Exception{
- DataNode n=cache.get(fqn);
+ DataNode n=cache.peek(Fqn.fromString(fqn));
IdentityLock lock=n.getLock();
if(owner == null)
owner=Thread.currentThread();
@@ -415,6 +418,71 @@
assertTrue("owner " + owner + "is not owner", lock.isOwner(owner));
}
+ public void testConcurrentNodeAccessOnRemovalWithTx() throws Exception
+ {
+ cache.put("/a/b/c", null);
+ tx.begin();
+ cache.remove("/a/b/c");
+ // this node should now be locked.
+ Transaction t = cache.getTransactionManager().suspend();
+ Transaction t2 = null;
+ try
+ {
+ System.out.println(cache.printLockInfo());
+ // start a new tx
+ cache.getTransactionManager().begin();
+ t2 = cache.getTransactionManager().getTransaction();
+ cache.get("/a/b/c"); // should fail
+ t2.commit();
+ fail("Should not be able to get a hold of /a/b/c until the deleting tx completes");
+ }
+ catch (Exception e)
+ {
+ // expected
+ t2.commit();
+ }
+
+ cache.getTransactionManager().resume(t);
+ tx.rollback();
+
+ assertNotNull(cache.get("/a/b/c"));
+ assertEquals(0, cache.getNumberOfLocksHeld());
+ }
+
+ public void testConcurrentNodeAccessOnRemovalWithoutTx() throws Exception
+ {
+ cache.put("/a/b/c", null);
+ tx.begin();
+ cache.remove("/a/b/c");
+ // this node should now be locked.
+ Transaction t = cache.getTransactionManager().suspend();
+ Thread th = new Thread(){
+ public void run(){
+ try
+ {
+ System.out.println(cache.printLockInfo());
+ cache.get("/a/b/c"); // should fail
+
+ fail("Should not be able to get a hold of /a/b/c until the deleting tx completes");
+ }
+ catch (Exception e)
+ {
+ // expected
+ }
+ }
+ };
+
+ th.start();
+ th.join();
+
+ cache.getTransactionManager().resume(t);
+ tx.rollback();
+
+ assertNotNull(cache.get("/a/b/c"));
+ assertEquals(0, cache.getNumberOfLocksHeld());
+ }
+
+
public void testRemove() throws CacheException, SystemException, NotSupportedException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
cache.put("/a/b/c", null);
@@ -429,7 +497,8 @@
tx.begin();
cache.remove("/a/b/c");
System.out.println("locks held (after removing /a/b/c): \n" + cache.printLockInfo());
- assertEquals(2, cache.getNumberOfLocksHeld());
+ // this used to test for 2 locks held. After the fixes for JBCACHE-875 however, 2 more locks are acquired - for the root node as well as the deleted node.
+ assertEquals(4, cache.getNumberOfLocksHeld());
tx.commit();
System.out.println("locks held (after committing /a/b/c): \n" + cache.printLockInfo());
assertEquals(0, cache.getNumberOfLocksHeld());
@@ -450,7 +519,7 @@
System.out.println("locks held (before removing /a/b/c): \n" + cache.printLockInfo());
cache.remove("/a/b/c");
System.out.println("locks held (after removing /a/b/c): \n" + cache.printLockInfo());
- assertEquals(2, cache.getNumberOfLocksHeld());
+ assertEquals(4, cache.getNumberOfLocksHeld());
tx.rollback();
System.out.println("locks held (after rollback): \n" + cache.printLockInfo());
assertEquals(0, cache.getNumberOfLocksHeld());
More information about the jboss-cvs-commits
mailing list