use of com.arjuna.ats.jta.distributed.spi.TxListener in project narayana by jbosstm.
the class TransactionListenerRegistryTest method test.
@Test
public void test() throws SystemException, NotSupportedException, RollbackException, TransactionTypeNotSupported, InterruptedException, InvalidTransactionException, HeuristicRollbackException, HeuristicMixedException {
TransactionManager tm = new TransactionManagerDelegate();
ServerVMClientUserTransaction userTransaction = new ServerVMClientUserTransaction(tm);
userTransaction.setTransactionTimeout(1);
for (CompletionType completionType : CompletionType.values()) {
TransactionListenerRegistry listenerRegistration = (TransactionListenerRegistry) tm;
userTransaction.begin();
// The TSR for interposed synchronizations
final TransactionSynchronizationRegistry tsr = new TransactionSynchronizationRegistryImple();
final TxListener listener = new TxListener(listenerRegistration);
if (completionType != CompletionType.CMTSUSPEND) {
tsr.registerInterposedSynchronization(listener);
} else {
tm.getTransaction().registerSynchronization(listener);
}
listenerRegistration.addListener(tm.getTransaction(), listener, EnumSet.allOf(EventType.class));
if (completionType == CompletionType.CMTSUSPEND) {
Transaction suspended = tm.suspend();
Thread.sleep(2000);
assertTrue(listener.shouldDisassoc());
tm.resume(suspended);
} else {
Thread.sleep(2000);
assertFalse(listener.shouldDisassoc());
}
assertTrue(listener.singleCallAC());
assertTrue(Status.STATUS_ROLLEDBACK == userTransaction.getStatus());
// https://community.jboss.org/thread/92489
if (completionType == CompletionType.BMTCOMMIT) {
try {
userTransaction.commit();
fail("Should not have been able to commit");
} catch (RollbackException e) {
}
} else if (completionType == CompletionType.BMTROLLBACK) {
userTransaction.rollback();
} else if (completionType == CompletionType.CMT) {
// This is possible in CMT mode
// If they did check the status, it is still expected that a CMT
// calls suspend at least when a tx is marked as completed to
// clear
// it from the thread
tm.suspend();
}
assertTrue(listener.singleCallAC());
if (completionType == CompletionType.CMTSUSPEND) {
assertFalse(listener.shouldDisassoc());
} else {
assertTrue(listener.shouldDisassoc());
}
assertTrue(listener.isClosed());
}
}
Aggregations