use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testAndThenRemoveOnFailure.
@Test
public void testAndThenRemoveOnFailure() throws TransactionFailureException {
ds1.failCommitTxOnce = InduceFailure.ThrowException;
TransactionContext context = newTransactionContext();
context.start();
Assert.assertTrue(context.addTransactionAware(ds1));
ds1.addChange(A);
try {
context.finish();
Assert.fail("Persist should have failed - exception should be thrown");
} catch (TransactionFailureException e) {
Assert.assertEquals("persist failure", e.getCause().getMessage());
}
Assert.assertTrue(context.removeTransactionAware(ds1));
// Verify ds1 is rolled back
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds1.committed);
Assert.assertFalse(ds1.postCommitted);
Assert.assertTrue(ds1.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testChangesAndRollbackFailure.
@Test
public void testChangesAndRollbackFailure() throws TransactionFailureException, InterruptedException {
ds1.failChangesTxOnce = InduceFailure.ThrowException;
ds1.failRollbackTxOnce = InduceFailure.ThrowException;
TransactionContext context = newTransactionContext(ds1, ds2);
// start transaction
context.start();
// add a change to ds1 and ds2
ds1.addChange(A);
ds2.addChange(B);
// commit transaction should fail and cause rollback
try {
context.finish();
Assert.fail("get changes failed - exception should be thrown");
} catch (TransactionFailureException e) {
Assert.assertEquals("changes failure", e.getCause().getMessage());
}
// verify both are rolled back and tx is invalidated
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertFalse(ds2.checked);
Assert.assertFalse(ds1.committed);
Assert.assertFalse(ds2.committed);
Assert.assertFalse(ds1.postCommitted);
Assert.assertFalse(ds2.postCommitted);
Assert.assertTrue(ds1.rolledBack);
Assert.assertTrue(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Invalidated);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testCommitFalse.
@Test
public void testCommitFalse() throws TransactionFailureException, InterruptedException {
txClient.failCommits = 1;
TransactionContext context = newTransactionContext(ds1, ds2);
// start transaction
context.start();
// add a change to ds1 and ds2
ds1.addChange(A);
ds2.addChange(B);
// commit transaction should fail and cause rollback
try {
context.finish();
Assert.fail("commit failed - exception should be thrown");
} catch (TransactionConflictException e) {
Assert.assertNull(e.getCause());
}
// verify both are rolled back and tx is aborted
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds2.checked);
Assert.assertTrue(ds1.committed);
Assert.assertTrue(ds2.committed);
Assert.assertFalse(ds1.postCommitted);
Assert.assertFalse(ds2.postCommitted);
Assert.assertTrue(ds1.rolledBack);
Assert.assertTrue(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testAbortFailureThrowsFailureException.
@Test
public void testAbortFailureThrowsFailureException() throws TransactionFailureException {
TransactionContext context = new SimpleTransactionContext(new FailingTxClient(txManager));
context.start();
Assert.assertTrue(context.addTransactionAware(ds1));
ds1.addChange(A);
try {
context.finish();
Assert.fail("Finish should have failed - exception should be thrown");
} catch (TransactionFailureException e) {
// expected
}
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testPostCommitFailure.
@Test
public void testPostCommitFailure() throws TransactionFailureException, InterruptedException {
ds1.failPostCommitTxOnce = InduceFailure.ThrowException;
TransactionContext context = newTransactionContext(ds1, ds2);
// start transaction
context.start();
// add a change to ds1 and ds2
ds1.addChange(A);
ds2.addChange(B);
// commit transaction should fail but without rollback as the failure happens post-commit
try {
context.finish();
Assert.fail("post commit failed - exception should be thrown");
} catch (TransactionFailureException e) {
Assert.assertEquals("post failure", e.getCause().getMessage());
}
// verify both are committed and post-committed
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds2.checked);
Assert.assertTrue(ds1.committed);
Assert.assertTrue(ds2.committed);
Assert.assertTrue(ds1.postCommitted);
Assert.assertTrue(ds2.postCommitted);
Assert.assertFalse(ds1.rolledBack);
Assert.assertFalse(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Committed);
}
Aggregations