Search in sources :

Example 51 with ParsedStatement

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));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 52 with ParsedStatement

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());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 53 with ParsedStatement

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));
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) TransactionManager(com.google.cloud.spanner.TransactionManager) TransactionContext(com.google.cloud.spanner.TransactionContext) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 54 with ParsedStatement

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()));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 55 with ParsedStatement

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());
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Aggregations

ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)75 Test (org.junit.Test)61 Statement (com.google.cloud.spanner.Statement)23 SpannerException (com.google.cloud.spanner.SpannerException)20 ResultSet (com.google.cloud.spanner.ResultSet)12 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 TimestampBound (com.google.cloud.spanner.TimestampBound)6 LinkedList (java.util.LinkedList)3 Mockito.anyString (org.mockito.Mockito.anyString)3 AbortedException (com.google.cloud.spanner.AbortedException)2 QueryOption (com.google.cloud.spanner.Options.QueryOption)2 Mutation (com.google.cloud.spanner.Mutation)1 ReadContext (com.google.cloud.spanner.ReadContext)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionManager (com.google.cloud.spanner.TransactionManager)1 ImmutableList (com.google.common.collect.ImmutableList)1 Duration (com.google.protobuf.Duration)1 BigDecimal (java.math.BigDecimal)1 TimeUnit (java.util.concurrent.TimeUnit)1