[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/transaction ...
Manik Surtani
msurtani at jboss.com
Thu Dec 14 12:18:48 EST 2006
User: msurtani
Date: 06/12/14 12:18:48
Modified: tests/functional/org/jboss/cache/transaction
IsolationLevelReadCommittedTest.java
TransactionTest.java
Log:
The beginnings of porting JBCACHE-871 and JBCACHE-875 as well as rearranging the Node/Cache object model to something sensible
Revision Changes Path
1.10 +17 -19 JBossCache/tests/functional/org/jboss/cache/transaction/IsolationLevelReadCommittedTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: IsolationLevelReadCommittedTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/transaction/IsolationLevelReadCommittedTest.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- IsolationLevelReadCommittedTest.java 22 Nov 2006 04:55:44 -0000 1.9
+++ IsolationLevelReadCommittedTest.java 14 Dec 2006 17:18:48 -0000 1.10
@@ -6,11 +6,9 @@
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-
import org.jboss.cache.Cache;
import org.jboss.cache.DummyTransactionManagerLookup;
import org.jboss.cache.Fqn;
-import org.jboss.cache.TreeCache;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Configuration.CacheMode;
import org.jboss.cache.factories.DefaultCacheFactory;
@@ -25,7 +23,7 @@
* Tests READ_COMMITED isolation level.
*
* @author <a href="mailto:ovidiu at jboss.org">Ovidiu Feodorov</a>
- * @version $Id: IsolationLevelReadCommittedTest.java,v 1.9 2006/11/22 04:55:44 bstansberry Exp $
+ * @version $Id: IsolationLevelReadCommittedTest.java,v 1.10 2006/12/14 17:18:48 msurtani Exp $
*/
public class IsolationLevelReadCommittedTest extends TestCase
@@ -101,7 +99,7 @@
tx = startTransaction();
// Read the value, thus acquiring a read lock on FQN
- assertEquals(VALUE, cache.get(FQN, KEY));
+ assertEquals("Could not read node with expected value!", VALUE, cache.get(FQN, KEY));
writerCanWrite.release();
@@ -110,15 +108,13 @@
readerCanRead.acquire();
// I shouldn't be able to see the "dirty" value
- assertEquals("thread w/ read lock " +
- "can see subsequent uncommitted changes",
- VALUE, cache.get(FQN, KEY));
+ assertEquals("thread w/ read lock can see subsequent uncommitted changes!!", VALUE, cache.get(FQN, KEY));
// let the writer know it can rollback
writerCanRollback.release();
// I should still be able to see the "clean" value
- assertEquals(VALUE, cache.get(FQN, KEY));
+ assertEquals("Could not read node with expected value!", VALUE, cache.get(FQN, KEY));
}
catch (AssertionFailedError e)
{
@@ -133,7 +129,9 @@
{
System.out.println("reader thread exits");
if (tx != null)
+ {
try { tx.commit(); } catch (Exception e) {}
+ }
writerCanWrite.release();
writerCanRollback.release();
readerDone.release();
@@ -253,7 +251,7 @@
{
writerError = e;
}
- catch(Throwable t)
+ catch (Throwable t)
{
t.printStackTrace();
writerFailed = true;
1.19 +150 -152 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.18
retrieving revision 1.19
diff -u -b -r1.18 -r1.19
--- TransactionTest.java 6 Dec 2006 12:46:53 -0000 1.18
+++ TransactionTest.java 14 Dec 2006 17:18:48 -0000 1.19
@@ -38,7 +38,7 @@
* Tests transactional access to a local TreeCache.
* Note: we use DummpyTranasctionManager to replace jta
*
- * @version $Id: TransactionTest.java,v 1.18 2006/12/06 12:46:53 msurtani Exp $
+ * @version $Id: TransactionTest.java,v 1.19 2006/12/14 17:18:48 msurtani Exp $
*/
public class TransactionTest extends TestCase
{
@@ -109,26 +109,19 @@
}
- public void testPutTx()
- {
- try
+ public void testPutTx() throws Exception
{
tx.begin();
- cache.put("/a/b/c", "age", new Integer(38));
+ cache.put("/a/b/c", "age", 38);
// the tx interceptor should know that we're in the same tx.
- assertEquals(new Integer(38), cache.get("/a/b/c", "age"));
+ assertEquals(cache.get("/a/b/c", "age"), new Integer(38));
- cache.put("/a/b/c", "age", new Integer(39));
+ cache.put("/a/b/c", "age", 39);
tx.commit();
// This test is done outside the TX, it wouldn't work if someone else
// modified "age". This works because we're the only TX running.
- assertEquals(new Integer(39), cache.get("/a/b/c", "age"));
- }
- catch (Throwable t)
- {
- fail(t.toString());
- }
+ assertEquals(cache.get("/a/b/c", "age"), new Integer(39));
}
@@ -155,7 +148,9 @@
public void testGetAfterRemovalRollback() throws Exception
{
+ assertEquals(0, cache.getNumberOfLocksHeld());
cache.put("/a/b", null);
+ assertEquals(0, cache.getNumberOfLocksHeld());
assertTrue(cache.exists("/a/b"));
tx.begin();
cache.remove("/a/b");
@@ -409,7 +404,8 @@
cache.put("/a/b/c", null);
tx.begin();
gtx = cache.getCurrentTransaction();
- cache.remove("/a/b/c"); // need to remove the node, not just the data in the node.
+ cache.remove("/a/b/c");// need to remove the node, not just the data in the node.
+ System.out.println("Locks: " + cache.printLockInfo());
assertLocked(gtx, "/a", false);
assertLocked(gtx, "/a/b", true);
assertLocked(gtx, "/a/b/c", true);
@@ -431,7 +427,7 @@
cache.put("/a/b/c", null);
tx.begin();
gtx = cache.getCurrentTransaction();
- cache.remove("/a/b"); // need to remove the node, not just the data in the node.
+ cache.remove("/a/b");// need to remove the node, not just the data in the node.
assertLocked(gtx, "/a", true);
assertLocked(gtx, "/a/b", true);
assertLocked(gtx, "/a/b/c", true);
@@ -576,10 +572,12 @@
private void assertLocked(Object owner, String fqn, boolean write_locked) throws Exception
{
- NodeImpl n = cache.get(fqn);
+ NodeImpl n = (NodeImpl) cache.peek(Fqn.fromString(fqn));
IdentityLock lock = n.getLock();
if (owner == null)
+ {
owner = Thread.currentThread();
+ }
assertTrue("node " + fqn + " is not locked", lock.isLocked());
if (write_locked)
{
@@ -606,7 +604,7 @@
// start a new tx
cache.getTransactionManager().begin();
t2 = cache.getTransactionManager().getTransaction();
- cache.get("/a/b/c"); // should fail
+ 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");
}
@@ -637,7 +635,7 @@
try
{
System.out.println(cache.printLockInfo());
- cache.get("/a/b/c"); // should fail
+ 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");
}
@@ -793,7 +791,7 @@
{
try
{
- cache.put("/bela/ban", null); // create a node /bela/ban with a null map
+ cache.put("/bela/ban", null);// create a node /bela/ban with a null map
tx.begin();
Map m = new HashMap();
m.put("name", "Bela");
@@ -855,7 +853,7 @@
{
try
{
- cache.put("/bela/ban", null); // /bela/ban needs to exist
+ cache.put("/bela/ban", null);// /bela/ban needs to exist
tx.begin();
cache.put("/bela/ban", "name", "Bela");
assertEquals("Bela", cache.get("/bela/ban", "name"));
@@ -874,7 +872,7 @@
{
try
{
- cache.put("/bela/ban", "name", "Bela"); // /bela/ban needs to exist
+ cache.put("/bela/ban", "name", "Bela");// /bela/ban needs to exist
tx.begin();
cache.put("/bela/ban", "name", "Michelle");
assertEquals("Michelle", cache.get("/bela/ban", "name"));
@@ -906,7 +904,7 @@
cache.remove(FQN);
cache.put(FQN, "entry", "rollback");
tx.rollback();
- assertEquals("Node should keep the commited value", "commit", cache.get(FQN).get("entry")); // THIS FAILS
+ assertEquals("Node should keep the commited value", "commit", cache.get(FQN).get("entry"));// THIS FAILS
}
@@ -924,7 +922,7 @@
cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
cache.start();
- cache.put("/1/2/3/4", "foo", "bar"); // no TX, no locks held after put() returns
+ cache.put("/1/2/3/4", "foo", "bar");// no TX, no locks held after put() returns
class Reader extends Thread
@@ -942,11 +940,11 @@
{
thread_tx = startTransaction();
log("acquiring RL");
- cache.get("/1/2/3", "foo"); // acquires RLs on all 3 nodes
+ cache.get("/1/2/3", "foo");// acquires RLs on all 3 nodes
log("RL acquired successfully");
sleep(2000);
log("committing TX");
- thread_tx.commit(); // releases RLs
+ thread_tx.commit();// releases RLs
log("committed TX");
}
catch (Exception e)
@@ -969,10 +967,10 @@
{
try
{
- sleep(500); // give the Reader a chance to acquire the RLs
+ sleep(500);// give the Reader a chance to acquire the RLs
thread_tx = startTransaction();
log("acquiring WL");
- cache.put("/1", "foo", "bar2"); // needs to acquired a WL on /1
+ cache.put("/1", "foo", "bar2");// needs to acquired a WL on /1
log("acquired WL successfully");
log("committing TX");
thread_tx.commit();
@@ -1046,126 +1044,126 @@
System.out.println(Thread.currentThread().getName() + ": " + msg);
}
-// public void testRaceConditionOnNotInCacheCondition() throws Exception {
-// cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
-//
-// tx.begin();
-// // we now read the null entry, and decide that we need to go do something.
-//
-// Object cachedObject=cache.get("/SecurityInfo/", Integer.toString(23));
-// assertNull(cachedObject); // we expect this in this test
-//
-// /**
-// * now start another Thread to go do the same action, looking for the value, but it SHOULD
-// * see the result of the main thread put once it commits.
-// */
-// Thread thread=new Thread(new Runnable() {
-// UserTransaction tx2=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
-// public void run() {
-// try {
-// tx2.begin();
-// log("OtherThread: inspecting the cache");
-// Object cachedObject=cache.get("/SecurityInfo", Integer.toString(23));
-//
-// log("OtherThread: read from cache: " + cachedObject);
-// Thread.sleep(3000);
-//
-// cachedObject=cache.get("/SecurityInfo", Integer.toString(23));
-// log("OtherThread: read(second time) from cache:" + cachedObject);
-//
-// /**
-// * This should really fail because the other thread should actually have put something else there.
-// */
-// cache.put("/SecurityInfo", Integer.toString(23), "HelloWorldDIRTY!");
-//
-// log("OtherThread: Has put something in the cache tha shouldn't be there");
-// }
-// catch(Exception e) {
-// e.printStackTrace();
-// }
-// finally {
-// if(tx2 != null)
-// try {tx2.commit();} catch(Exception e) {e.printStackTrace();}
-// }
-// log("OthreThread: exiting");
-//
-// }
-// });
-//
-// thread.start();
-// log("MainThread is now waiting a little bit");
-// Thread.sleep(2000); // wait long enough for the other thread to block a bit. Simulate a DB read
-// log("MainThread is now putting something in the cache");
-// cache.put("/SecurityInfo", Integer.toString(23), "HelloWorld");
-// Thread.sleep(2000); // wait long enough for the other thread to block a bit. Simulate a DB read
-// tx.commit();
-// log("MainThread: committed");
-// thread.join(30000);
-//
-// log(cache.get("/SecurityInfo", Integer.toString(23)).toString());
-// }
-
-// public void testConcurrentReadsWithSerializableIsolationLevel() throws CacheException, InterruptedException {
-// final long TIMEOUT=5000;
-// cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
-// cache.put("/testfqn", "testkey", "testvalue"); // add initial value, to be read by threads
-// Reader r1, r2;
-// r1=new Reader("reader1", 3000);
-// r2=new Reader("reader2", 0);
-// r1.start();
-// pause(100); // make sure thread1 starts and acquires the lock before thread2
-// r2.start();
-// r1.join(TIMEOUT);
-// r2.join(TIMEOUT);
-// }
-//
-// class Reader extends Thread {
-// long timeout;
-//
-// public Reader(String name, long timeout) {
-// super(name);
-// this.timeout=timeout;
-// }
-//
-// public void run() {
-// UserTransaction trans=null;
-// try {
-// trans=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
-// trans.begin();
-// Object retval=null;
-// log2("accessing tree");
-// retval=cache.get("testfqn", "testkey");
-// log2("retval: " + retval);
-// if(timeout > 0) {
-// log2("sleeping for " + timeout + " ms");
-// pause(timeout);
-// }
-// }
-// catch(Exception e) {
-// e.printStackTrace();
-// }
-// finally {
-// try {trans.commit();} catch(Throwable t) {}
-// log2("done");
-// }
-// }
-// }
-
-// private void pause(long timeout) {
-// try {
-// Thread.sleep(timeout);
-// }
-// catch(InterruptedException e) {
-// }
-// }
-//
-// private void log(String msg) {
-// System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
-// }
-//
-// private void log2(String msg) {
-// System.out.println("-- [" + System.currentTimeMillis() + " " + Thread.currentThread() + "]: " + msg);
-// }
+ // public void testRaceConditionOnNotInCacheCondition() throws Exception {
+ // cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
+ //
+ // tx.begin();
+ // // we now read the null entry, and decide that we need to go do something.
+ //
+ // Object cachedObject=cache.get("/SecurityInfo/", Integer.toString(23));
+ // assertNull(cachedObject); // we expect this in this test
+ //
+ // /**
+ // * now start another Thread to go do the same action, looking for the value, but it SHOULD
+ // * see the result of the main thread put once it commits.
+ // */
+ // Thread thread=new Thread(new Runnable() {
+ // UserTransaction tx2=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
+ // public void run() {
+ // try {
+ // tx2.begin();
+ // log("OtherThread: inspecting the cache");
+ // Object cachedObject=cache.get("/SecurityInfo", Integer.toString(23));
+ //
+ // log("OtherThread: read from cache: " + cachedObject);
+ // Thread.sleep(3000);
+ //
+ // cachedObject=cache.get("/SecurityInfo", Integer.toString(23));
+ // log("OtherThread: read(second time) from cache:" + cachedObject);
+ //
+ // /**
+ // * This should really fail because the other thread should actually have put something else there.
+ // */
+ // cache.put("/SecurityInfo", Integer.toString(23), "HelloWorldDIRTY!");
+ //
+ // log("OtherThread: Has put something in the cache tha shouldn't be there");
+ // }
+ // catch(Exception e) {
+ // e.printStackTrace();
+ // }
+ // finally {
+ // if(tx2 != null)
+ // try {tx2.commit();} catch(Exception e) {e.printStackTrace();}
+ // }
+ // log("OthreThread: exiting");
+ //
+ // }
+ // });
+ //
+ // thread.start();
+ // log("MainThread is now waiting a little bit");
+ // Thread.sleep(2000); // wait long enough for the other thread to block a bit. Simulate a DB read
+ // log("MainThread is now putting something in the cache");
+ // cache.put("/SecurityInfo", Integer.toString(23), "HelloWorld");
+ // Thread.sleep(2000); // wait long enough for the other thread to block a bit. Simulate a DB read
+ // tx.commit();
+ // log("MainThread: committed");
+ // thread.join(30000);
+ //
+ // log(cache.get("/SecurityInfo", Integer.toString(23)).toString());
+ // }
+
+ // public void testConcurrentReadsWithSerializableIsolationLevel() throws CacheException, InterruptedException {
+ // final long TIMEOUT=5000;
+ // cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
+ // cache.put("/testfqn", "testkey", "testvalue"); // add initial value, to be read by threads
+ // Reader r1, r2;
+ // r1=new Reader("reader1", 3000);
+ // r2=new Reader("reader2", 0);
+ // r1.start();
+ // pause(100); // make sure thread1 starts and acquires the lock before thread2
+ // r2.start();
+ // r1.join(TIMEOUT);
+ // r2.join(TIMEOUT);
+ // }
+ //
+ // class Reader extends Thread {
+ // long timeout;
+ //
+ // public Reader(String name, long timeout) {
+ // super(name);
+ // this.timeout=timeout;
+ // }
+ //
+ // public void run() {
+ // UserTransaction trans=null;
+ // try {
+ // trans=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
+ // trans.begin();
+ // Object retval=null;
+ // log2("accessing tree");
+ // retval=cache.get("testfqn", "testkey");
+ // log2("retval: " + retval);
+ // if(timeout > 0) {
+ // log2("sleeping for " + timeout + " ms");
+ // pause(timeout);
+ // }
+ // }
+ // catch(Exception e) {
+ // e.printStackTrace();
+ // }
+ // finally {
+ // try {trans.commit();} catch(Throwable t) {}
+ // log2("done");
+ // }
+ // }
+ // }
+
+ // private void pause(long timeout) {
+ // try {
+ // Thread.sleep(timeout);
+ // }
+ // catch(InterruptedException e) {
+ // }
+ // }
+ //
+ // private void log(String msg) {
+ // System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
+ // }
+ //
+ // private void log2(String msg) {
+ // System.out.println("-- [" + System.currentTimeMillis() + " " + Thread.currentThread() + "]: " + msg);
+ // }
public static Test suite() throws Exception
More information about the jboss-cvs-commits
mailing list