use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewOnExistingRecordsWithSerializableWithExtraRead_OneShouldCommitTheOtherShouldThrowCommitConflictException.
private void commit_WriteSkewOnExistingRecordsWithSerializableWithExtraRead_OneShouldCommitTheOtherShouldThrowCommitConflictException(String table1, String table2) throws TransactionException {
// Arrange
List<Put> puts = Arrays.asList(preparePut(0, 0, table1).withValue(BALANCE, 1), preparePut(0, 1, table2).withValue(BALANCE, 1));
GrpcTransaction transaction = manager.start();
transaction.put(puts);
transaction.commit();
// Act
GrpcTransaction transaction1 = manager.start();
GrpcTransaction transaction2 = manager.start();
Get get1_1 = prepareGet(0, 1, table2);
Optional<Result> result1 = transaction1.get(get1_1);
assertThat(result1).isPresent();
int current1 = getBalance(result1.get());
Get get1_2 = prepareGet(0, 0, table1);
transaction1.get(get1_2);
Get get2_1 = prepareGet(0, 0, table1);
Optional<Result> result2 = transaction2.get(get2_1);
assertThat(result2).isPresent();
int current2 = getBalance(result2.get());
Get get2_2 = prepareGet(0, 1, table2);
transaction2.get(get2_2);
Put put1 = preparePut(0, 0, table1).withValue(BALANCE, current1 + 1);
transaction1.put(put1);
Put put2 = preparePut(0, 1, table2).withValue(BALANCE, current2 + 1);
transaction2.put(put2);
transaction1.commit();
Throwable thrown = catchThrowable(transaction2::commit);
// Assert
transaction = manager.start();
result1 = transaction.get(get1_1);
result2 = transaction.get(get2_1);
transaction.commit();
assertThat(result1).isPresent();
assertThat(getBalance(result1.get())).isEqualTo(1);
assertThat(result2).isPresent();
assertThat(getBalance(result2.get())).isEqualTo(2);
assertThat(thrown).isInstanceOf(CommitConflictException.class);
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForNonExisting_ShouldCreateRecord.
@Test
public void putAndCommit_PutGivenForNonExisting_ShouldCreateRecord() throws TransactionException {
// Arrange
int expected = INITIAL_BALANCE;
Put put = preparePut(0, 0, TABLE_1).withValue(BALANCE, expected);
GrpcTransaction transaction = manager.start();
// Act
transaction.put(put);
transaction.commit();
// Assert
Get get = prepareGet(0, 0, TABLE_1);
GrpcTransaction another = manager.start();
Optional<Result> result = another.get(get);
another.commit();
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(expected);
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class DistributedStorageServiceTest method mutate_StorageThrowsIllegalArgumentException_ShouldThrowInvalidArgumentError.
@Test
public void mutate_StorageThrowsIllegalArgumentException_ShouldThrowInvalidArgumentError() throws ExecutionException {
// Arrange
Key partitionKey = new Key("col1", 1);
MutateRequest request = MutateRequest.newBuilder().addMutation(ProtoUtils.toMutation(new Put(partitionKey))).build();
@SuppressWarnings("unchecked") StreamObserver<Empty> responseObserver = mock(StreamObserver.class);
doThrow(IllegalArgumentException.class).when(storage).mutate(anyList());
// Act
storageService.mutate(request, responseObserver);
// Assert
verify(responseObserver).onError(exceptionCaptor.capture());
assertThat(exceptionCaptor.getValue().getStatus().getCode()).isEqualTo(Status.Code.INVALID_ARGUMENT);
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class DistributedStorageServiceTest method mutate_IsCalledWithMixedPutAndDelete_StorageShouldBeCalledProperly.
@Test
public void mutate_IsCalledWithMixedPutAndDelete_StorageShouldBeCalledProperly() throws ExecutionException {
// Arrange
Key partitionKey = new Key("col1", 1);
MutateRequest request = MutateRequest.newBuilder().addAllMutation(Arrays.asList(ProtoUtils.toMutation(new Put(partitionKey)), ProtoUtils.toMutation(new Delete(partitionKey)))).build();
@SuppressWarnings("unchecked") StreamObserver<Empty> responseObserver = mock(StreamObserver.class);
// Act
storageService.mutate(request, responseObserver);
// Assert
verify(storage).mutate(anyList());
verify(responseObserver).onNext(any());
verify(responseObserver).onCompleted();
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class DistributedStorageServiceTest method mutate_IsCalledWithSinglePut_StorageShouldBeCalledProperly.
@Test
public void mutate_IsCalledWithSinglePut_StorageShouldBeCalledProperly() throws ExecutionException {
// Arrange
Key partitionKey = new Key("col1", 1);
MutateRequest request = MutateRequest.newBuilder().addMutation(ProtoUtils.toMutation(new Put(partitionKey))).build();
@SuppressWarnings("unchecked") StreamObserver<Empty> responseObserver = mock(StreamObserver.class);
// Act
storageService.mutate(request, responseObserver);
// Assert
verify(storage).mutate(anyList());
verify(responseObserver).onNext(any());
verify(responseObserver).onCompleted();
}
Aggregations