@Test
public void testTransactionWatcher() throws Exception {
final String txType = "someName";
final long txid = 7;
final SimpleArbitrator sa = new SimpleArbitrator();
final TransactionWatcher txw = new TransactionWatcher(sa);
sa.start(txType, txid);
try {
sa.start(txType, txid);
Assert.fail("simple arbitrator did not throw an exception");
} catch (Exception ex) {
// expected
}
txw.isActive(txid);
Assert.assertFalse(txw.isActive(txid));
txw.run(txType, txid, new Callable<Object>() {
@Override
public Object call() throws Exception {
Assert.assertTrue(txw.isActive(txid));
return null;
}
});
Assert.assertFalse(txw.isActive(txid));
Assert.assertFalse(sa.transactionComplete(txType, txid));
sa.stop(txType, txid);
Assert.assertFalse(sa.transactionAlive(txType, txid));
Assert.assertFalse(sa.transactionComplete(txType, txid));
sa.cleanup(txType, txid);
Assert.assertTrue(sa.transactionComplete(txType, txid));
try {
txw.run(txType, txid, new Callable<Object>() {
@Override
public Object call() throws Exception {
Assert.fail("Should not be able to start a new work on a discontinued transaction");
return null;
}
});
Assert.fail("work against stopped transaction should fail");
} catch (Exception ex) {
;
}
final long txid2 = 9;
sa.start(txType, txid2);
txw.run(txType, txid2, new Callable<Object>() {
@Override
public Object call() throws Exception {
Assert.assertTrue(txw.isActive(txid2));
sa.stop(txType, txid2);
try {
txw.run(txType, txid2, new Callable<Object>() {
@Override
public Object call() throws Exception {
Assert.fail("Should not be able to start a new work on a discontinued transaction");
return null;
}
});
Assert.fail("work against a stopped transaction should fail");
} catch (Exception ex) {
// expected
}
Assert.assertTrue(txw.isActive(txid2));
return null;
}
});
}