[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/replicated ...
Manik Surtani
msurtani at belmont.prod.atl2.jboss.com
Wed Aug 30 13:08:19 EDT 2006
User: msurtani
Date: 06/08/30 13:08:19
Modified: tests/functional/org/jboss/cache/replicated
SyncReplTxTest.java
Log:
Java5 optimisations
Revision Changes Path
1.13 +623 -525 JBossCache/tests/functional/org/jboss/cache/replicated/SyncReplTxTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: SyncReplTxTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/replicated/SyncReplTxTest.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- SyncReplTxTest.java 16 Aug 2006 10:52:52 -0000 1.12
+++ SyncReplTxTest.java 30 Aug 2006 17:08:19 -0000 1.13
@@ -36,14 +36,15 @@
* Note: we use DummyTransactionManager for Tx purpose instead of relying on
* jta.
*
- * @version $Revision: 1.12 $
+ * @version $Revision: 1.13 $
*/
-public class SyncReplTxTest extends TestCase {
+public class SyncReplTxTest extends TestCase
+{
private static Log log = LogFactory.getLog(SyncReplTxTest.class);
TreeCache cache1, cache2;
- Configuration.CacheMode caching_mode= Configuration.CacheMode.REPL_SYNC;
- final String group_name="TreeCacheTestGroup";
- String props=
+ Configuration.CacheMode caching_mode = Configuration.CacheMode.REPL_SYNC;
+ final String group_name = "TreeCacheTestGroup";
+ String props =
"UDP(ip_mcast=true;ip_ttl=64;loopback=false;mcast_addr=228.1.2.3;" +
"mcast_port=45566;mcast_recv_buf_size=80000;mcast_send_buf_size=150000;" +
"ucast_recv_buf_size=80000;ucast_send_buf_size=150000):" +
@@ -59,48 +60,52 @@
"pbcast.GMS(join_retry_timeout=2000;join_timeout=5000;print_local_addr=true;shun=true):" +
"pbcast.STATE_TRANSFER(down_thread=true;up_thread=true)";
- final static Log log_=LogFactory.getLog(SyncReplTxTest.class);
- String old_factory=null;
- final String FACTORY="org.jboss.cache.transaction.DummyContextFactory";
- FIFOSemaphore lock=new FIFOSemaphore(1);
+ final static Log log_ = LogFactory.getLog(SyncReplTxTest.class);
+ String old_factory = null;
+ final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
+ FIFOSemaphore lock = new FIFOSemaphore(1);
DummyTransactionManager tx_mgr;
- Throwable t1_ex, t2_ex, ex=null;
+ Throwable t1_ex, t2_ex, ex = null;
-
- public SyncReplTxTest(String name) {
+ public SyncReplTxTest(String name)
+ {
super(name);
}
- public void setUp() throws Exception {
+ public void setUp() throws Exception
+ {
super.setUp();
- old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
+ old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
- tx_mgr=DummyTransactionManager.getInstance();
- t1_ex=t2_ex=ex=null;
+ tx_mgr = DummyTransactionManager.getInstance();
+ t1_ex = t2_ex = ex = null;
}
- public void tearDown() throws Exception {
+ public void tearDown() throws Exception
+ {
super.tearDown();
DummyTransactionManager.destroy();
destroyCaches();
- if(old_factory != null) {
+ if (old_factory != null)
+ {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
- old_factory=null;
+ old_factory = null;
}
}
- Transaction beginTransaction() throws SystemException, NotSupportedException {
- DummyTransactionManager mgr=DummyTransactionManager.getInstance();
+ Transaction beginTransaction() throws SystemException, NotSupportedException
+ {
+ DummyTransactionManager mgr = DummyTransactionManager.getInstance();
mgr.begin();
- Transaction tx=mgr.getTransaction();
- return tx;
+ return mgr.getTransaction();
}
- void initCaches(Configuration.CacheMode caching_mode) throws Exception {
- this.caching_mode=caching_mode;
- cache1=new TreeCache();
- cache2=new TreeCache();
+ void initCaches(Configuration.CacheMode caching_mode) throws Exception
+ {
+ this.caching_mode = caching_mode;
+ cache1 = new TreeCache();
+ cache2 = new TreeCache();
cache1.getConfiguration().setCacheMode(caching_mode);
cache2.getConfiguration().setCacheMode(caching_mode);
cache1.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
@@ -108,30 +113,28 @@
cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
- /*
- cache1.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
- cache2.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
-*/
cache1.getConfiguration().setLockAcquisitionTimeout(5000);
cache2.getConfiguration().setLockAcquisitionTimeout(5000);
cache1.start();
cache2.start();
}
- void destroyCaches() throws Exception {
- if(cache1 != null)
+ void destroyCaches() throws Exception
+ {
+ if (cache1 != null)
cache1.stop();
- if(cache2 != null)
+ if (cache2 != null)
cache2.stop();
- cache1=null;
- cache2=null;
+ cache1 = null;
+ cache2 = null;
}
- public void testLockRemoval() throws Exception {
+ public void testLockRemoval() throws Exception
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncCommitPhase(true);
cache1.releaseAllLocks("/");
- Transaction tx=beginTransaction();
+ Transaction tx = beginTransaction();
cache1.put("/bela/ban", "name", "Bela Ban");
assertEquals(2, cache1.getNumberOfLocksHeld());
assertEquals(0, cache2.getNumberOfLocksHeld());
@@ -141,20 +144,21 @@
}
-
- public void testSyncRepl() throws Exception {
+ public void testSyncRepl() throws Exception
+ {
Integer age;
Transaction tx;
- try {
+ try
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncCommitPhase(true);
cache2.getConfiguration().setSyncCommitPhase(true);
// assertEquals(2, cache1.getMembers().size());
- tx=beginTransaction();
- cache1.put("/a/b/c", "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put("/a/b/c", "age", 38);
TransactionManager mgr = cache1.getTransactionManager();
tx = mgr.suspend();
assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
@@ -166,46 +170,51 @@
log.debug("cache2: locks held after commit: " + cache2.printLockInfo());
// value on cache2 must be 38
- age=(Integer)cache2.get("/a/b/c", "age");
+ age = (Integer) cache2.get("/a/b/c", "age");
assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
- assertTrue("\"age\" must be 38", age.intValue() == 38);
+ assertTrue("\"age\" must be 38", age == 38);
}
- catch(Exception e) {
+ catch (Exception e)
+ {
fail(e.toString());
}
}
/**
- *
* @throws Exception
*/
- public void testSimplePut() throws Exception {
+ public void testSimplePut() throws Exception
+ {
Integer age;
- try {
+ try
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.put("/JSESSION/localhost/192.168.1.10:32882/Courses/0", "Instructor", "Ben Wang");
- cache1.put("/JSESSION/localhost/192.168.1.10:32882/1", "Number", new Integer(10));
+ cache1.put("/JSESSION/localhost/192.168.1.10:32882/1", "Number", 10);
}
- catch(Exception e) {
+ catch (Exception e)
+ {
fail(e.toString());
}
}
- public void testSimpleTxPut() throws Exception {
+ public void testSimpleTxPut() throws Exception
+ {
Transaction tx;
- final Fqn NODE1=Fqn.fromString("/one/two/three");
- final Fqn NODE2=Fqn.fromString("/one/two/three/four");
- final Fqn NODE3=Fqn.fromString("/one/two/three/fourI");
+ final Fqn NODE1 = Fqn.fromString("/one/two/three");
+ final Fqn NODE2 = Fqn.fromString("/one/two/three/four");
+ final Fqn NODE3 = Fqn.fromString("/one/two/three/fourI");
- try {
+ try
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
- tx=beginTransaction();
- cache1.put(NODE1, "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put(NODE1, "age", 38);
System.out.println("TransactionTable for cache1 after cache1.put():\n" + cache1.getTransactionTable().toString(true));
tx.commit();
@@ -229,28 +238,31 @@
System.out.println("TransactionTable for cache1 after cache1.put():\n" + cache1.getTransactionTable().toString(true));
*/
}
- catch(Exception e) {
+ catch (Exception e)
+ {
fail(e.toString());
}
}
- public void testSyncReplWithModficationsOnBothCaches() throws Exception {
+ public void testSyncReplWithModficationsOnBothCaches() throws Exception
+ {
Integer age;
Transaction tx;
- final Fqn NODE1=Fqn.fromString("/one/two/three");
- final Fqn NODE2=Fqn.fromString("/eins/zwei/drei");
+ final Fqn NODE1 = Fqn.fromString("/one/two/three");
+ final Fqn NODE2 = Fqn.fromString("/eins/zwei/drei");
- try {
+ try
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncCommitPhase(true);
cache2.getConfiguration().setSyncCommitPhase(true);
- tx=beginTransaction();
- cache1.put(NODE1, "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put(NODE1, "age", 38);
System.out.println("TransactionTable for cache1 after cache1.put():\n" + cache1.getTransactionTable().toString(true));
- cache2.put(NODE2, "age", new Integer(39));
+ cache2.put(NODE2, "age", 39);
System.out.println("TransactionTable for cache2 after cache2.put():\n" + cache2.getTransactionTable().toString(true));
System.out.println("cache1 before commit:\n" + cache1.printLockInfo());
@@ -266,53 +278,55 @@
assertTrue(cache1.exists(NODE1));
assertTrue(cache2.exists(NODE2));
- age=(Integer)cache1.get(NODE1, "age");
+ age = (Integer) cache1.get(NODE1, "age");
assertNotNull("\"age\" obtained from cache1 for " + NODE1 + " must be non-null ", age);
- assertTrue("\"age\" must be 38", age.intValue() == 38);
+ assertTrue("\"age\" must be 38", age == 38);
- age=(Integer)cache2.get(NODE1, "age");
+ age = (Integer) cache2.get(NODE1, "age");
assertNotNull("\"age\" obtained from cache2 for " + NODE1 + " must be non-null ", age);
- assertTrue("\"age\" must be 38", age.intValue() == 38);
+ assertTrue("\"age\" must be 38", age == 38);
- age=(Integer)cache1.get(NODE2, "age");
+ age = (Integer) cache1.get(NODE2, "age");
assertNotNull("\"age\" obtained from cache1 for " + NODE2 + " must be non-null ", age);
- assertTrue("\"age\" must be 39", age.intValue() == 39);
+ assertTrue("\"age\" must be 39", age == 39);
- age=(Integer)cache2.get(NODE2, "age");
+ age = (Integer) cache2.get(NODE2, "age");
assertNotNull("\"age\" obtained from cache2 for " + NODE2 + " must be non-null ", age);
- assertTrue("\"age\" must be 39", age.intValue() == 39);
+ assertTrue("\"age\" must be 39", age == 39);
assertEquals(0, cache1.getNumberOfLocksHeld());
assertEquals(0, cache2.getNumberOfLocksHeld());
System.out.println("TransactionTable for cache1:\n" + cache1.getTransactionTable().toString(true));
System.out.println("TransactionTable for cache2:\n" + cache2.getTransactionTable().toString(true));
}
- catch(Exception e) {
+ catch (Exception e)
+ {
fail(e.toString());
}
}
- public void testSyncReplWithModficationsOnBothCachesSameData() throws Exception {
+ public void testSyncReplWithModficationsOnBothCachesSameData() throws Exception
+ {
Transaction tx;
- final Fqn NODE=Fqn.fromString("/one/two/three");
-
- try {
+ final Fqn NODE = Fqn.fromString("/one/two/three");
initCaches(Configuration.CacheMode.REPL_SYNC);
- tx=beginTransaction();
- cache1.put(NODE, "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put(NODE, "age", 38);
System.out.println("TransactionTable for cache1 after cache1.put():\n" + cache1.getTransactionTable().toString(true));
- cache2.put(NODE, "age", new Integer(39));
+ cache2.put(NODE, "age", 39);
System.out.println("TransactionTable for cache2 after cache2.put():\n" + cache2.getTransactionTable().toString(true));
System.out.println("cache1 before commit:\n" + cache1.printLockInfo());
System.out.println("cache2 before commit:\n" + cache2.printLockInfo());
- try {
+ try
+ {
tx.commit();
fail("commit should throw a RollbackException, we should not get here");
}
- catch(RollbackException rollback) {
+ catch (RollbackException rollback)
+ {
System.out.println("Transaction was rolled back, this is correct");
}
@@ -325,26 +339,22 @@
assertEquals(0, cache1.getNumberOfNodes());
assertEquals(0, cache2.getNumberOfNodes());
}
- catch(Exception e) {
- fail(e.toString());
- }
- }
- public void testSyncReplWithModficationsOnBothCachesWithRollback() throws Exception {
+ public void testSyncReplWithModficationsOnBothCachesWithRollback() throws Exception
+ {
Transaction tx;
- final Fqn NODE1=Fqn.fromString("/one/two/three");
- final Fqn NODE2=Fqn.fromString("/eins/zwei/drei");
+ final Fqn NODE1 = Fqn.fromString("/one/two/three");
+ final Fqn NODE2 = Fqn.fromString("/eins/zwei/drei");
- try {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncRollbackPhase(true);
cache2.getConfiguration().setSyncRollbackPhase(true);
- tx=beginTransaction();
- cache1.put(NODE1, "age", new Integer(38));
- cache2.put(NODE2, "age", new Integer(39));
+ tx = beginTransaction();
+ cache1.put(NODE1, "age", 38);
+ cache2.put(NODE2, "age", 39);
System.out.println("cache1 (before commit):\n" + cache1.printLockInfo());
System.out.println("cache2 (before commit):\n" + cache2.printLockInfo());
@@ -352,11 +362,13 @@
// this will rollback the transaction
tx.registerSynchronization(new TransactionAborter(tx));
- try {
+ try
+ {
tx.commit();
fail("commit should throw a RollbackException, we should not get here");
}
- catch(RollbackException rollback) {
+ catch (RollbackException rollback)
+ {
System.out.println("Transaction was rolled back, this is correct");
}
@@ -369,10 +381,6 @@
assertEquals(0, cache1.getNumberOfNodes());
assertEquals(0, cache2.getNumberOfNodes());
}
- catch(Exception e) {
- fail(e.toString());
- }
- }
/**
@@ -381,11 +389,11 @@
*
* @throws Exception
*/
- public void testSyncReplWithRollbackAndListener() throws Exception {
+ public void testSyncReplWithRollbackAndListener() throws Exception
+ {
Transaction tx;
- final Fqn NODE1=Fqn.fromString("/one/two/three");
+ final Fqn NODE1 = Fqn.fromString("/one/two/three");
- try {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncRollbackPhase(true);
@@ -396,8 +404,8 @@
CallbackListener cbl1 = new CallbackListener(cache1, "age");
CallbackListener cbl2 = new CallbackListener(cache2, "age");
- tx=beginTransaction();
- cache1.put(NODE1, "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put(NODE1, "age", 38);
System.out.println("cache1 (before commit):\n" + cache1.printLockInfo());
System.out.println("cache2 (before commit):\n" + cache2.printLockInfo());
@@ -405,11 +413,13 @@
// this will rollback the transaction
tx.registerSynchronization(new TransactionAborter(tx));
- try {
+ try
+ {
tx.commit();
fail("commit should throw a RollbackException, we should not get here");
}
- catch(RollbackException rollback) {
+ catch (RollbackException rollback)
+ {
rollback.printStackTrace();
System.out.println("Transaction was rolled back, this is correct");
}
@@ -435,8 +445,8 @@
// listener aborts any active tx
cbl2 = new TransactionAborterCallbackListener(cache2, "age");
- tx=beginTransaction();
- cache1.put(NODE1, "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put(NODE1, "age", 38);
System.out.println("cache1 (before commit):\n" + cache1.printLockInfo());
System.out.println("cache2 (before commit):\n" + cache2.printLockInfo());
@@ -460,11 +470,6 @@
assertEquals(0, cache2.getNumberOfNodes());
}
- catch(Exception e) {
- e.printStackTrace();
- fail(e.toString());
- }
- }
/**
@@ -473,11 +478,11 @@
*
* @throws Exception
*/
- public void testSyncReplWithRemoteRollback() throws Exception {
+ public void testSyncReplWithRemoteRollback() throws Exception
+ {
Transaction tx;
- final Fqn NODE1=Fqn.fromString("/one/two/three");
+ final Fqn NODE1 = Fqn.fromString("/one/two/three");
- try {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncRollbackPhase(true);
@@ -488,17 +493,19 @@
// listener aborts any active tx
TransactionAborterListener tal = new TransactionAborterListener(cache2);
- tx=beginTransaction();
- cache1.put(NODE1, "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put(NODE1, "age", 38);
System.out.println("cache1 (before commit):\n" + cache1.printLockInfo());
System.out.println("cache2 (before commit):\n" + cache2.printLockInfo());
- try {
+ try
+ {
tx.commit();
fail("commit should throw a RollbackException, we should not get here");
}
- catch(RollbackException rollback) {
+ catch (RollbackException rollback)
+ {
System.out.println("Transaction was rolled back, this is correct");
}
@@ -517,36 +524,27 @@
assertEquals(0, cache2.getNumberOfNodes());
}
- catch(Exception e) {
- e.printStackTrace();
- fail(e.toString());
- }
- }
-
- public void testASyncRepl() throws Exception {
+ public void testASyncRepl() throws Exception
+ {
Integer age;
Transaction tx;
initCaches(Configuration.CacheMode.REPL_ASYNC);
- try {
- tx=beginTransaction();
- cache1.put("/a/b/c", "age", new Integer(38));
+ tx = beginTransaction();
+ cache1.put("/a/b/c", "age", 38);
Thread.sleep(1000);
assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
tx.commit();
Thread.sleep(1000);
// value on cache2 must be 38
- age=(Integer)cache2.get("/a/b/c", "age");
+ age = (Integer) cache2.get("/a/b/c", "age");
assertNotNull("\"age\" obtained from cache2 is null ", age);
- assertTrue("\"age\" must be 38", age.intValue() == 38);
- }
- catch(Exception e) {
- fail(e.toString());
- }
+ assertTrue("\"age\" must be 38", age == 38);
+
}
/**
@@ -582,38 +580,47 @@
* I'm currently investigating solution #2, ie. creating an OrderedSynchronizationHandler, which allows other
* SynchronizationHandlers to register (atHead, atTail), and the OrderedSynchronizationHandler would call the
* SynchronizationHandler in the order in which they are defined.
+ *
* @throws Exception
*/
- public void testConcurrentPuts() throws Exception {
+ public void testConcurrentPuts() throws Exception
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncCommitPhase(true);
- Thread t1=new Thread("Thread1") {
+ Thread t1 = new Thread("Thread1")
+ {
Transaction tx;
- public void run() {
- try {
- tx=beginTransaction();
+ public void run()
+ {
+ try
+ {
+ tx = beginTransaction();
cache1.put("/bela/ban", "name", "Bela Ban");
_pause(2000); // Thread2 will be blocked until we commit
tx.commit();
System.out.println("[Thread1] ** LOCK INFO cache1: " + cache1.printLockInfo());
System.out.println("[Thread1] ** LOCK INFO cache2: " + cache2.printLockInfo());
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t1_ex=ex;
+ t1_ex = ex;
}
}
};
- Thread t2=new Thread("Thread2") {
+ Thread t2 = new Thread("Thread2")
+ {
Transaction tx;
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
_pause(1000); // give Thread1 time to acquire the lock
- tx=beginTransaction();
+ tx = beginTransaction();
System.out.println("[Thread2] ** LOCK INFO cache1: " + cache1.printLockInfo());
System.out.println("[Thread2] ** LOCK INFO cache2: " + cache2.printLockInfo());
cache1.put("/bela/ban", "name", "Michelle Ban");
@@ -623,9 +630,10 @@
System.out.println("[Thread2] ** LOCK INFO cache1: " + cache1.printLockInfo());
System.out.println("[Thread2] ** LOCK INFO cache2: " + cache2.printLockInfo());
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t2_ex=ex;
+ t2_ex = ex;
}
}
};
@@ -638,9 +646,9 @@
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
fail("Thread1 failed: " + t1_ex);
- if(t2_ex != null)
+ if (t2_ex != null)
fail("Thread2 failed: " + t2_ex);
assertEquals("Michelle Ban", cache1.get("/bela/ban", "name"));
@@ -650,25 +658,28 @@
/**
* Should reproduce JBCACHE-32 problem (http://jira.jboss.com/jira/browse/JBCACHE-32)
*/
- public void testConcurrentCommitsWith1Thread() throws Exception {
+ public void testConcurrentCommitsWith1Thread() throws Exception
+ {
_testConcurrentCommits(1);
}
/**
* Should reproduce JBCACHE-32 problem (http://jira.jboss.com/jira/browse/JBCACHE-32)
*/
- public void testConcurrentCommitsWith5Threads() throws Exception {
+ public void testConcurrentCommitsWith5Threads() throws Exception
+ {
_testConcurrentCommits(5);
}
/**
* Should reproduce JBCACHE-32 problem (http://jira.jboss.com/jira/browse/JBCACHE-32)
*/
- private void _testConcurrentCommits(int num_threads) throws Exception {
- Object myMutex=new Object();
+ private void _testConcurrentCommits(int num_threads) throws Exception
+ {
+ Object myMutex = new Object();
- final TreeCache c1=new TreeCache();
- final TreeCache c2=new TreeCache();
+ final TreeCache c1 = new TreeCache();
+ final TreeCache c2 = new TreeCache();
c1.getConfiguration().setClusterName("TempCluster");
c2.getConfiguration().setClusterName("TempCluster");
c1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
@@ -684,59 +695,71 @@
c1.start();
c2.start();
- class MyThread extends Thread {
+ class MyThread extends Thread
+ {
Object mutex;
- public MyThread(String name, Object mutex) {
+ public MyThread(String name, Object mutex)
+ {
super(name);
- this.mutex=mutex;
+ this.mutex = mutex;
}
- public void run() {
+ public void run()
+ {
Transaction tx;
- try {
- tx=beginTransaction();
+ try
+ {
+ tx = beginTransaction();
c1.put("/thread/" + getName(), null);
System.out.println("Thread " + getName() + " after put(): " + c1.toString());
System.out.println("Thread " + getName() + " waiting on mutex");
- synchronized(mutex) {
+ synchronized (mutex)
+ {
mutex.wait();
}
System.out.println("Thread " + getName() + " committing");
tx.commit();
System.out.println("Thread " + getName() + " committed successfully");
}
- catch(Exception e) {
- ex=e;
+ catch (Exception e)
+ {
+ ex = e;
}
}
}
- MyThread[] threads=new MyThread[num_threads];
- for(int i=0; i < threads.length; i++) {
- threads[i]=new MyThread("#" + i, myMutex);
+ MyThread[] threads = new MyThread[num_threads];
+ for (int i = 0; i < threads.length; i++)
+ {
+ threads[i] = new MyThread("#" + i, myMutex);
}
- for(int i=0; i < threads.length; i++) {
- MyThread thread=threads[i];
+ for (int i = 0; i < threads.length; i++)
+ {
+ MyThread thread = threads[i];
System.out.println("starting thread #" + i);
thread.start();
}
_pause(6000);
- synchronized(myMutex) {
+ synchronized (myMutex)
+ {
System.out.println("cache is " + c1.printLockInfo());
System.out.println("******************* SIGNALLING THREADS ********************");
myMutex.notifyAll();
}
- for(int i=0; i < threads.length; i++) {
- MyThread thread=threads[i];
- try {
+ for (int i = 0; i < threads.length; i++)
+ {
+ MyThread thread = threads[i];
+ try
+ {
thread.join();
System.out.println("Joined thread " + thread.getName());
}
- catch(InterruptedException e) {
+ catch (InterruptedException e)
+ {
e.printStackTrace();
}
}
@@ -749,7 +772,7 @@
c1.stop();
c2.stop();
- if(ex != null)
+ if (ex != null)
fail("Thread failed: " + ex);
}
@@ -757,44 +780,54 @@
/**
* Conncurrent put on 2 different instances.
*/
- public void testConcurrentPutsOnTwoInstances() throws Exception {
+ public void testConcurrentPutsOnTwoInstances() throws Exception
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
- final TreeCache c1=this.cache1;
- final TreeCache c2=this.cache2;
+ final TreeCache c1 = this.cache1;
+ final TreeCache c2 = this.cache2;
- Thread t1=new Thread() {
+ Thread t1 = new Thread()
+ {
Transaction tx;
- public void run() {
- try {
- tx=beginTransaction();
+ public void run()
+ {
+ try
+ {
+ tx = beginTransaction();
c1.put("/ben/wang", "name", "Ben Wang");
_pause(8000);
tx.commit(); // This should go thru
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t1_ex=ex;
+ t1_ex = ex;
}
}
};
- Thread t2=new Thread() {
+ Thread t2 = new Thread()
+ {
Transaction tx;
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
_pause(1000); // give Thread1 time to acquire the lock
- tx=beginTransaction();
+ tx = beginTransaction();
c2.put("/ben/wang", "name", "Ben Jr.");
tx.commit(); // This will time out and rollback first because Thread1 has a tx going as well.
}
- catch(RollbackException rollback_ex) {
+ catch (RollbackException rollback_ex)
+ {
System.out.println("received rollback exception as expected");
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t2_ex=ex;
+ t2_ex = ex;
}
}
};
@@ -807,25 +840,29 @@
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
fail("Thread1 failed: " + t1_ex);
- if(t2_ex != null)
+ if (t2_ex != null)
fail("Thread2 failed: " + t2_ex);
assertEquals("Ben Wang", c1.get("/ben/wang", "name"));
}
- public void testPut() throws Exception {
+ public void testPut() throws Exception
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
- final TreeCache c1=this.cache1;
+ final TreeCache c1 = this.cache1;
- Thread t1=new Thread() {
- public void run() {
- try {
+ Thread t1 = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
lock.acquire();
System.out.println("-- t1 has lock");
- c1.put("/a/b/c", "age", new Integer(38));
+ c1.put("/a/b/c", "age", 38);
System.out.println("[Thread1] set value to 38");
System.out.println("-- t1 releases lock");
@@ -835,32 +872,37 @@
lock.acquire();
System.out.println("-- t1 has lock");
- c1.put("/a/b/c", "age", new Integer(39));
+ c1.put("/a/b/c", "age", 39);
System.out.println("[Thread1] set value to 39");
System.out.println("-- t1 releases lock");
lock.release();
- assertEquals(new Integer(39), c1.get("/a/b/c", "age"));
+ assertEquals(39, c1.get("/a/b/c", "age"));
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t1_ex=ex;
+ t1_ex = ex;
}
- finally {
+ finally
+ {
lock.release();
}
}
};
- Thread t2=new Thread() {
- public void run() {
- try {
+ Thread t2 = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
_pause(100);
Thread.yield();
lock.acquire();
System.out.println("-- t2 has lock");
// Should replicate the value right away.
- Integer val=(Integer)cache2.get("/a/b/c", "age");
+ Integer val = (Integer) cache2.get("/a/b/c", "age");
System.out.println("[Thread2] value is " + val);
assertEquals(new Integer(38), val);
System.out.println("-- t2 releases lock");
@@ -870,16 +912,18 @@
_pause(500);
lock.acquire();
System.out.println("-- t2 has lock");
- val=(Integer)cache2.get("/a/b/c", "age");
+ val = (Integer) cache2.get("/a/b/c", "age");
System.out.println("-- t2 releases lock");
lock.release();
assertEquals(new Integer(39), val);
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t2_ex=ex;
+ t2_ex = ex;
}
- finally {
+ finally
+ {
lock.release();
}
}
@@ -892,9 +936,9 @@
// Wait for thread to die but put an insurance of 5 seconds on it.
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
fail("Thread1 failed: " + t1_ex);
- if(t2_ex != null)
+ if (t2_ex != null)
fail("Thread2 failed: " + t2_ex);
}
@@ -907,29 +951,33 @@
*
* @throws Exception
*/
- public void testPutTx() throws Exception {
- Transaction tx=null;
+ public void testPutTx() throws Exception
+ {
+ Transaction tx = null;
- try {
+ try
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
cache1.getConfiguration().setSyncCommitPhase(true);
cache2.getConfiguration().setSyncCommitPhase(true);
- tx=beginTransaction();
- cache1.put("/a/b/c", "age", new Integer(38));
- cache1.put("/a/b/c", "age", new Integer(39));
- Object val=cache2.get("/a/b/c", "age"); // must be null as not yet committed
+ tx = beginTransaction();
+ cache1.put("/a/b/c", "age", 38);
+ cache1.put("/a/b/c", "age", 39);
+ Object val = cache2.get("/a/b/c", "age"); // must be null as not yet committed
assertNull(val);
tx.commit();
- tx=beginTransaction();
- assertEquals(new Integer(39), cache2.get("/a/b/c", "age")); // must not be null
+ tx = beginTransaction();
+ assertEquals(39, cache2.get("/a/b/c", "age")); // must not be null
tx.commit();
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
- t1_ex=t;
+ t1_ex = t;
}
- finally {
+ finally
+ {
lock.release();
}
}
@@ -941,72 +989,85 @@
* cache2 commit will succeed since now that cache1 is rollbacked and lock is
* released.
*/
- public void testPutTx1() throws Exception {
+ public void testPutTx1() throws Exception
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
- final TreeCache c1=this.cache1;
- Thread t1=new Thread() {
- public void run() {
- Transaction tx=null;
+ final TreeCache c1 = this.cache1;
+ Thread t1 = new Thread()
+ {
+ public void run()
+ {
+ Transaction tx = null;
- try {
+ try
+ {
lock.acquire();
- tx=beginTransaction();
- c1.put("/a/b/c", "age", new Integer(38));
- c1.put("/a/b/c", "age", new Integer(39));
+ tx = beginTransaction();
+ c1.put("/a/b/c", "age", 38);
+ c1.put("/a/b/c", "age", 39);
lock.release();
_pause(300);
lock.acquire();
- try {
+ try
+ {
tx.commit();
}
- catch(RollbackException ex) {
+ catch (RollbackException ex)
+ {
System.out.println("[Thread1] received RollbackException, as expected. Rolling back changes");
- return;
}
- finally {
+ finally
+ {
lock.release();
}
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t1_ex=ex;
+ t1_ex = ex;
}
- finally {
+ finally
+ {
lock.release();
}
}
};
- Thread t2=new Thread() {
- public void run() {
- Transaction tx=null;
+ Thread t2 = new Thread()
+ {
+ public void run()
+ {
+ Transaction tx = null;
- try {
+ try
+ {
sleep(200);
Thread.yield();
lock.acquire();
- tx=beginTransaction();
+ tx = beginTransaction();
assertNull(cache2.get("/a/b/c", "age")); // must be null as not yet committed
- cache2.put("/a/b/c", "age", new Integer(40));
+ cache2.put("/a/b/c", "age", 40);
lock.release();
_pause(300);
lock.acquire();
- assertEquals(new Integer(40), cache2.get("/a/b/c", "age")); // must not be null
+ assertEquals(40, cache2.get("/a/b/c", "age")); // must not be null
tx.commit();
lock.release();
_pause(1000);
- tx=beginTransaction();
- assertEquals("After cache2 commit", new Integer(40), cache2.get("/a/b/c", "age"));
+ tx = beginTransaction();
+ assertEquals("After cache2 commit", 40, cache2.get("/a/b/c", "age"));
tx.commit();
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t2_ex=ex;
+ t2_ex = ex;
}
- finally {
+ finally
+ {
lock.release();
}
}
@@ -1019,26 +1080,29 @@
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
fail("Thread1 failed: " + t1_ex);
- if(t2_ex != null)
+ if (t2_ex != null)
fail("Thread2 failed: " + t2_ex);
}
-
- public void testPutTxWithRollback() throws Exception {
+ public void testPutTxWithRollback() throws Exception
+ {
initCaches(Configuration.CacheMode.REPL_SYNC);
- final TreeCache c2=this.cache1;
- Thread t1=new Thread() {
- public void run() {
- Transaction tx=null;
+ final TreeCache c2 = this.cache1;
+ Thread t1 = new Thread()
+ {
+ public void run()
+ {
+ Transaction tx = null;
- try {
+ try
+ {
lock.acquire();
- tx=beginTransaction();
- c2.put("/a/b/c", "age", new Integer(38));
- c2.put("/a/b/c", "age", new Integer(39));
+ tx = beginTransaction();
+ c2.put("/a/b/c", "age", 38);
+ c2.put("/a/b/c", "age", 39);
lock.release();
_pause(100);
@@ -1046,25 +1110,30 @@
tx.rollback();
lock.release();
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t1_ex=ex;
+ t1_ex = ex;
}
- finally {
+ finally
+ {
lock.release();
}
}
};
- Thread t2=new Thread() {
- public void run() {
- Transaction tx=null;
+ Thread t2 = new Thread()
+ {
+ public void run()
+ {
+ Transaction tx = null;
- try {
+ try
+ {
sleep(200);
Thread.yield();
lock.acquire();
- tx=beginTransaction();
+ tx = beginTransaction();
assertNull(cache2.get("/a/b/c", "age")); // must be null as not yet committed
lock.release();
@@ -1074,11 +1143,13 @@
tx.commit();
lock.release();
}
- catch(Throwable ex) {
+ catch (Throwable ex)
+ {
ex.printStackTrace();
- t2_ex=ex;
+ t2_ex = ex;
}
- finally {
+ finally
+ {
lock.release();
}
}
@@ -1091,40 +1162,48 @@
// Wait for thread to die but put an insurance of 5 seconds on it.
t1.join();
t2.join();
- if(t1_ex != null)
+ if (t1_ex != null)
fail("Thread1 failed: " + t1_ex);
- if(t2_ex != null)
+ if (t2_ex != null)
fail("Thread2 failed: " + t2_ex);
}
- static class TransactionAborter implements Synchronization {
- Transaction ltx=null;
+ static class TransactionAborter implements Synchronization
+ {
+ Transaction ltx = null;
- public TransactionAborter(Transaction ltx) {
- this.ltx=ltx;
+ public TransactionAborter(Transaction ltx)
+ {
+ this.ltx = ltx;
}
- public void beforeCompletion() {
- try {
+ public void beforeCompletion()
+ {
+ try
+ {
ltx.setRollbackOnly();
}
- catch(SystemException e) {
+ catch (SystemException e)
+ {
}
}
- public void afterCompletion(int status) {
+ public void afterCompletion(int status)
+ {
}
}
- static class CallbackListener extends AbstractCacheListener {
+ static class CallbackListener extends AbstractCacheListener
+ {
TreeCache callbackCache;
Object callbackKey;
Exception ex;
Object mutex = new Object();
- CallbackListener(TreeCache cache, Object callbackKey) {
+ CallbackListener(TreeCache cache, Object callbackKey)
+ {
this.callbackCache = cache;
this.callbackKey = callbackKey;
cache.getNotifier().addCacheListener(this);
@@ -1136,7 +1215,8 @@
{
// Lock on a mutex so test can't check for an exception
// until the get call completes
- synchronized(mutex) {
+ synchronized (mutex)
+ {
try
{
callbackCache.get(fqn, callbackKey);
@@ -1150,19 +1230,23 @@
}
}
- Exception getCallbackException() {
- synchronized(mutex) {
+ Exception getCallbackException()
+ {
+ synchronized (mutex)
+ {
return ex;
}
}
}
- static class TransactionAborterCallbackListener extends CallbackListener {
+ static class TransactionAborterCallbackListener extends CallbackListener
+ {
TransactionManager callbackTM;
- TransactionAborterCallbackListener(TreeCache cache, Object callbackKey) {
+ TransactionAborterCallbackListener(TreeCache cache, Object callbackKey)
+ {
super(cache, callbackKey);
callbackTM = callbackCache.getTransactionManager();
}
@@ -1174,11 +1258,13 @@
try
{
Transaction tx = callbackTM.getTransaction();
- if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
+ if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE)
+ {
// this will rollback the transaction
tx.registerSynchronization(new TransactionAborter(tx));
}
- else {
+ else
+ {
super.nodeModified(fqn, pre, isLocal, data);
}
@@ -1194,25 +1280,30 @@
}
- static class TransactionAborterListener extends AbstractCacheListener {
+ static class TransactionAborterListener extends AbstractCacheListener
+ {
TransactionManager callbackTM;
Object mutex = new Object();
Exception ex;
- TransactionAborterListener(TreeCache cache) {
+ TransactionAborterListener(TreeCache cache)
+ {
callbackTM = cache.getTransactionManager();
cache.getNotifier().addCacheListener(this);
}
public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, Map data)
{
- if (!pre) {
- synchronized(mutex) {
+ if (!pre)
+ {
+ synchronized (mutex)
+ {
try
{
Transaction tx = callbackTM.getTransaction();
- if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
+ if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE)
+ {
// this will rollback the transaction
tx.setRollbackOnly();
}
@@ -1223,11 +1314,14 @@
if (ex == null)
ex = e;
}
- } }
+ }
+ }
}
- Exception getCallbackException() {
- synchronized(mutex) {
+ Exception getCallbackException()
+ {
+ synchronized (mutex)
+ {
return ex;
}
}
@@ -1235,15 +1329,19 @@
}
- static void _pause(long millis) {
- try {
+ static void _pause(long millis)
+ {
+ try
+ {
Thread.sleep(millis);
}
- catch(Exception t) {
+ catch (Exception t)
+ {
}
}
- public static Test suite() throws Exception {
+ public static Test suite() throws Exception
+ {
// return getDeploySetup(SyncTxUnitTestCase.class, "cachetest.jar");
return new TestSuite(SyncReplTxTest.class);
}
More information about the jboss-cvs-commits
mailing list