[jboss-cvs] JBossCache/tests-50/functional/org/jboss/cache/pojo/rollback ...
Ben Wang
bwang at jboss.com
Sun Jul 30 22:17:34 EDT 2006
User: bwang
Date: 06/07/30 22:17:34
Added: tests-50/functional/org/jboss/cache/pojo/rollback
ReplicatedTxTest.java
Log:
Test cases for undo operation.
Revision Changes Path
1.1 date: 2006/07/31 02:17:34; author: bwang; state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/rollback/ReplicatedTxTest.java
Index: ReplicatedTxTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.rollback;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.pojo.*;
import org.jboss.cache.pojo.test.Person;
import org.jboss.cache.pojo.test.Student;
import org.jboss.cache.transaction.DummyTransactionManager;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import javax.transaction.SystemException;
import javax.transaction.NotSupportedException;
import javax.transaction.Transaction;
import javax.transaction.RollbackException;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
/**
*/
public class ReplicatedTxTest extends TestCase
{
Log log = LogFactory.getLog(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class);
PojoCache cache, cache1;
final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
DummyTransactionManager tx_mgr;
Throwable t1_ex, t2_ex;
long start = 0;
public ReplicatedTxTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/replSync-service.xml";
cache = PojoCacheFactory.createInstance(configFile);
cache.start();
cache1 = PojoCacheFactory.createInstance(configFile);
cache1.start();
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
tx_mgr = DummyTransactionManager.getInstance();
t1_ex = t2_ex = null;
}
protected void tearDown() throws Exception
{
super.tearDown();
cache.stop();
cache1.stop();
DummyTransactionManager.destroy();
}
// public void testDummy() {}
UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
{
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.cache.transaction.DummyContextFactory");
return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
}
private Person createPerson(String id, String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
return p;
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
UserTransaction tx = getTransaction();
tx.begin();
Person p = createPerson("/person/test1", "Harald Gliebe", 32);
cache.attach("/person/test1", p);
tx.commit();
tx.begin();
p.setName("Benoit");
tx.commit();
Person p1 = (Person) cache1.find("/person/test1");
assertEquals("Benoit", p.getName());
assertEquals("Benoit", p1.getName());
tx.begin();
p1.setAge(61);
tx.commit();
assertEquals(61, p.getAge());
assertEquals(61, p1.getAge());
}
/**
* Concurrent puts (whole POJO) from the same cache instance (different threads) with rollback.
*/
public void testConcurrentPuts() throws Exception
{
Thread t1 = new Thread()
{
Transaction tx;
public void run()
{
try
{
Person p = createPerson("/person/test6", "p6", 50);
List<String> lang = new ArrayList<String>();
lang.add("German");
p.setLanguages(lang);
UserTransaction tx = getTransaction();
tx.begin();
cache.attach("/person/test6", p);
TestingUtil.sleepThread(17000);
tx.commit();
}
catch (RollbackException rollback)
{
;
}
catch (Exception ex)
{
t1_ex = ex;
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
UserTransaction tx = null;
Person p = createPerson("/person/test6", "p6", 50);
try
{
TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
List<String> lang = new ArrayList<String>();
lang.add("German");
p.setLanguages(lang);
tx = getTransaction();
tx.begin();
cache.attach("/person/test6", p);
tx.commit();
}
catch (RollbackException rollback)
{
;
}
catch (Exception ex)
{
try
{
tx.rollback();
} catch (SystemException e)
{
e.printStackTrace();
t2_ex = e;
}
}
cache.attach("/person/test6", p);
}
};
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if (t2_ex != null)
fail("Thread1 failed: " + t2_ex);
if (t1_ex != null)
fail("Thread2 failed: " + t1_ex);
int size = ((Person) cache.find("/person/test6")).getLanguages().size();
assertEquals("number of languages", 1, size);
size = ((Person) cache1.find("/person/test6")).getLanguages().size();
assertEquals("number of languages", 1, size);
}
/**
* Concurrent puts from the same cache instance (different threads) with rollback.
*/
public void testConcurrentPuts1() throws Exception
{
Thread t1 = new Thread()
{
Transaction tx;
public void run()
{
try
{
List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
UserTransaction tx = getTransaction();
tx.begin();
lang.add("German");
TestingUtil.sleepThread(17000);
tx.commit();
}
catch (RollbackException rollback)
{
;
}
catch (Exception ex)
{
t1_ex = ex;
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
UserTransaction tx = null;
try
{
TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
List<String> lang = ((Person) cache1.find("/person/test6")).getLanguages();
tx = getTransaction();
tx.begin();
lang.add("English");
tx.commit();
}
catch (RollbackException rollback)
{
;
}
catch (Exception ex)
{
try
{
tx.rollback();
} catch (SystemException e)
{
e.printStackTrace();
t2_ex = e;
}
}
}
};
Person p = createPerson("/person/test6", "p6", 50);
cache.attach("/person/test6", p);
List<String> lang = new ArrayList<String>();
lang.add("German");
p.setLanguages(lang);
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if (t2_ex != null)
fail("Thread1 failed: " + t2_ex);
if (t1_ex != null)
fail("Thread2 failed: " + t1_ex);
int size = ((Person) cache.find("/person/test6")).getLanguages().size();
assertEquals("number of languages", 2, size);
size = ((Person) cache1.find("/person/test6")).getLanguages().size();
assertEquals("number of languages", 2, size);
}
/**
* Concurrent puts from the different cache instances (different threads) with rollback.
*/
public void testConcurrentPuts2() throws Exception
{
Thread t1 = new Thread()
{
Transaction tx;
public void run()
{
try
{
List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
UserTransaction tx = getTransaction();
tx.begin();
lang.add("German");
TestingUtil.sleepThread(17000);
tx.commit();
}
catch (RollbackException rollback)
{
;
}
catch (Exception ex)
{
t1_ex = ex;
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
UserTransaction tx = null;
try
{
TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
tx = getTransaction();
tx.begin();
lang.add("English");
tx.commit();
}
catch (RollbackException rollback)
{
;
}
catch (Exception ex)
{
try
{
tx.rollback();
} catch (SystemException e)
{
e.printStackTrace();
t2_ex = e;
}
}
}
};
Person p = createPerson("/person/test6", "p6", 50);
cache.attach("/person/test6", p);
List<String> lang = new ArrayList<String>();
lang.add("German");
p.setLanguages(lang);
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if (t2_ex != null)
fail("Thread1 failed: " + t2_ex);
if (t1_ex != null)
fail("Thread2 failed: " + t1_ex);
int size = ((Person) cache.find("/person/test6")).getLanguages().size();
assertEquals("number of languages", 2, size);
size = ((Person) cache1.find("/person/test6")).getLanguages().size();
assertEquals("number of languages", 2, size);
}
void log(String s)
{
long now;
if (start == 0)
start = System.currentTimeMillis();
now = System.currentTimeMillis();
System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
}
public static Test suite() throws Exception
{
return new TestSuite(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(org.jboss.cache.pojo.rollback.ReplicatedTxTest.suite());
}
}
More information about the jboss-cvs-commits
mailing list