use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method prepare_DeleteGivenWithoutRead_ShouldThrowPreparationException.
@Test
public void prepare_DeleteGivenWithoutRead_ShouldThrowPreparationException() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.delete(prepareDelete(0, 0, TABLE_1));
// Act
Throwable throwable = catchThrowable(transaction::prepare);
transaction.rollback();
// Assert
assertThat(throwable).isInstanceOf(PreparationException.class);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method get_PutCalledBefore_ShouldGet.
@Test
public void get_PutCalledBefore_ShouldGet() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
// Act
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
Get get = prepareGet(0, 0, TABLE_1);
Optional<Result> result = transaction.get(get);
assertThatCode(() -> {
transaction.prepare();
transaction.commit();
}).doesNotThrowAnyException();
// Assert
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(1);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_NonConflictingPutsGivenForExisting_ShouldCommitBoth.
@Test
public void commit_NonConflictingPutsGivenForExisting_ShouldCommitBoth() throws TransactionException {
// Arrange
int amount1 = 100;
int amount2 = 200;
int fromId = 0;
int fromType = 0;
int toId = 1;
int toType = 0;
int anotherFromId = 2;
int anotherFromType = 0;
int anotherToId = 3;
int anotherToType = 0;
populate(TABLE_1);
populate(TABLE_2);
GrpcTwoPhaseCommitTransaction fromTx = manager.start();
GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
transfer(fromId, fromType, TABLE_1, fromTx, toId, toType, TABLE_2, toTx, amount1);
// Act Assert
assertThatCode(() -> {
GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
transfer(anotherFromId, anotherFromType, TABLE_2, anotherFromTx, anotherToId, anotherToType, TABLE_1, anotherToTx, amount2);
anotherFromTx.prepare();
anotherToTx.prepare();
anotherFromTx.commit();
anotherToTx.commit();
}).doesNotThrowAnyException();
assertThatCode(() -> {
fromTx.prepare();
toTx.prepare();
fromTx.commit();
toTx.commit();
}).doesNotThrowAnyException();
// Assert
GrpcTwoPhaseCommitTransaction another = manager.start();
Optional<Result> result = another.get(prepareGet(fromId, fromType, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount1);
result = another.get(prepareGet(toId, toType, TABLE_2));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount1);
result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount2);
result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount2);
another.prepare();
another.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException.
@Test
public void scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
// Act Assert
assertThatThrownBy(() -> transaction.scan(prepareScan(0, 0, 0, TABLE_1))).isInstanceOf(IllegalArgumentException.class);
transaction.rollback();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_DeleteCalledBefore_ShouldReturnEmpty.
@Test
public void scan_DeleteCalledBefore_ShouldReturnEmpty() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction.prepare();
transaction.commit();
// Act Assert
GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
List<Result> resultBefore = transaction1.scan(prepareScan(0, 0, 0, TABLE_1));
transaction1.delete(prepareDelete(0, 0, TABLE_1));
List<Result> resultAfter = transaction1.scan(prepareScan(0, 0, 0, TABLE_1));
assertThatCode(() -> {
transaction1.prepare();
transaction1.commit();
}).doesNotThrowAnyException();
// Assert
assertThat(resultBefore.size()).isEqualTo(1);
assertThat(resultAfter.size()).isEqualTo(0);
}
Aggregations