use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class DmlBatchTest method testGetStateAndIsActive.
@Test
public void testGetStateAndIsActive() {
DmlBatch batch = createSubject();
assertThat(batch.getState(), is(UnitOfWorkState.STARTED));
assertThat(batch.isActive(), is(true));
get(batch.runBatchAsync());
assertThat(batch.getState(), is(UnitOfWorkState.RAN));
assertThat(batch.isActive(), is(false));
batch = createSubject();
assertThat(batch.getState(), is(UnitOfWorkState.STARTED));
assertThat(batch.isActive(), is(true));
batch.abortBatch();
assertThat(batch.getState(), is(UnitOfWorkState.ABORTED));
assertThat(batch.isActive(), is(false));
UnitOfWork tx = mock(UnitOfWork.class);
when(tx.executeBatchUpdateAsync(anyList())).thenReturn(ApiFutures.immediateFailedFuture(mock(SpannerException.class)));
batch = createSubject(tx);
assertThat(batch.getState(), is(UnitOfWorkState.STARTED));
assertThat(batch.isActive(), is(true));
ParsedStatement statement = mock(ParsedStatement.class);
when(statement.getStatement()).thenReturn(Statement.of("UPDATE TEST SET COL1=2"));
when(statement.getSqlWithoutComments()).thenReturn("UPDATE TEST SET COL1=2");
when(statement.getType()).thenReturn(StatementType.UPDATE);
get(batch.executeUpdateAsync(statement));
boolean exception = false;
try {
get(batch.runBatchAsync());
} catch (SpannerException e) {
exception = true;
}
assertThat(exception, is(true));
assertThat(batch.getState(), is(UnitOfWorkState.RUN_FAILED));
assertThat(batch.isActive(), is(false));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testGetCommitResponseBeforeCommit.
@Test
public void testGetCommitResponseBeforeCommit() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.UPDATE);
when(parsedStatement.isUpdate()).thenReturn(true);
Statement statement = Statement.of("UPDATE FOO SET BAR=1 WHERE ID=2");
when(parsedStatement.getStatement()).thenReturn(statement);
ReadWriteTransaction transaction = createSubject();
get(transaction.executeUpdateAsync(parsedStatement));
try {
transaction.getCommitResponse();
fail("Expected exception");
} catch (SpannerException ex) {
assertEquals(ErrorCode.FAILED_PRECONDITION, ex.getErrorCode());
}
assertNull(transaction.getCommitResponseOrNull());
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testRetry.
@Test
public void testRetry() {
for (RetryResults results : RetryResults.values()) {
String sql1 = "UPDATE FOO SET BAR=1 WHERE BAZ>=100 AND BAZ<200";
String sql2 = "UPDATE FOO SET BAR=2 WHERE BAZ>=200 AND BAZ<300";
DatabaseClient client = mock(DatabaseClient.class);
ParsedStatement update1 = mock(ParsedStatement.class);
when(update1.getType()).thenReturn(StatementType.UPDATE);
when(update1.isUpdate()).thenReturn(true);
when(update1.getStatement()).thenReturn(Statement.of(sql1));
ParsedStatement update2 = mock(ParsedStatement.class);
when(update2.getType()).thenReturn(StatementType.UPDATE);
when(update2.isUpdate()).thenReturn(true);
when(update2.getStatement()).thenReturn(Statement.of(sql2));
TransactionManager txManager = mock(TransactionManager.class);
TransactionContext txContext1 = mock(TransactionContext.class);
when(txManager.begin()).thenReturn(txContext1);
when(txManager.getState()).thenReturn(null, TransactionState.STARTED);
when(client.transactionManager()).thenReturn(txManager);
when(txContext1.executeUpdate(Statement.of(sql1))).thenReturn(90L);
when(txContext1.executeUpdate(Statement.of(sql2))).thenReturn(80L);
TransactionContext txContext2 = mock(TransactionContext.class);
when(txManager.resetForRetry()).thenReturn(txContext2);
when(client.transactionManager()).thenReturn(txManager);
if (results == RetryResults.SAME) {
when(txContext2.executeUpdate(Statement.of(sql1))).thenReturn(90L);
when(txContext2.executeUpdate(Statement.of(sql2))).thenReturn(80L);
} else if (results == RetryResults.DIFFERENT) {
when(txContext2.executeUpdate(Statement.of(sql1))).thenReturn(90L);
when(txContext2.executeUpdate(Statement.of(sql2))).thenReturn(90L);
}
// first abort, then do nothing
doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, "commit aborted", createAbortedExceptionWithMinimalRetry())).doNothing().when(txManager).commit();
ReadWriteTransaction subject = ReadWriteTransaction.newBuilder().setRetryAbortsInternally(true).setTransactionRetryListeners(Collections.emptyList()).setDatabaseClient(client).withStatementExecutor(new StatementExecutor()).build();
subject.executeUpdateAsync(update1);
subject.executeUpdateAsync(update2);
boolean expectedException = false;
try {
get(subject.commitAsync());
} catch (SpannerException e) {
if (results == RetryResults.DIFFERENT && e.getErrorCode() == ErrorCode.ABORTED) {
// expected
expectedException = true;
} else {
throw e;
}
}
assertThat(expectedException, is(results == RetryResults.DIFFERENT));
}
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testGetCommitTimestampAfterCommit.
@Test
public void testGetCommitTimestampAfterCommit() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.UPDATE);
when(parsedStatement.isUpdate()).thenReturn(true);
Statement statement = Statement.of("UPDATE FOO SET BAR=1 WHERE ID=2");
when(parsedStatement.getStatement()).thenReturn(statement);
ReadWriteTransaction transaction = createSubject();
assertThat(get(transaction.executeUpdateAsync(parsedStatement)), is(1L));
get(transaction.commitAsync());
assertThat(transaction.getCommitTimestamp(), is(notNullValue()));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testExecuteDdl.
@Test
public void testExecuteDdl() {
ParsedStatement statement = mock(ParsedStatement.class);
when(statement.getType()).thenReturn(StatementType.DDL);
ReadWriteTransaction transaction = createSubject();
try {
transaction.executeDdlAsync(statement);
fail("Expected exception");
} catch (SpannerException ex) {
assertEquals(ErrorCode.FAILED_PRECONDITION, ex.getErrorCode());
}
}
Aggregations