Search in sources :

Example 6 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testAbortedDuringRetryOfFailedUpdateAsFirstStatement.

@Test
public void testAbortedDuringRetryOfFailedUpdateAsFirstStatement() {
    final Statement invalidStatement = Statement.of("INSERT INTO FOO");
    StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
    mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
    try (ITConnection connection = createConnection(createAbortRetryListener(2, invalidStatement, notFound))) {
        try {
            connection.execute(invalidStatement);
            fail("missing expected exception");
        } catch (SpannerException e) {
            assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
        }
        connection.execute(INSERT_STATEMENT);
        // Force an abort and retry.
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
    assertThat(mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)).isEqualTo(8);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Statement(com.google.cloud.spanner.Statement) StatusRuntimeException(io.grpc.StatusRuntimeException) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 7 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method createConnection.

ITConnection createConnection(TransactionRetryListener listener) {
    ITConnection connection = super.createConnection(ImmutableList.of(), ImmutableList.of(listener));
    connection.setAutocommit(false);
    return connection;
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection)

Example 8 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testAbortedDuringRetryOfFailedQuery.

@Test
public void testAbortedDuringRetryOfFailedQuery() {
    final Statement invalidStatement = Statement.of("SELECT * FROM FOO");
    StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
    mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
    try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
        connection.execute(INSERT_STATEMENT);
        try (ResultSet rs = connection.executeQuery(invalidStatement)) {
            fail("missing expected exception");
        } catch (SpannerException e) {
            assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
        }
        // Force an abort and retry.
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
    // The transaction will be executed 3 times, which means that there will be 6
    // ExecuteSqlRequests:
    // 1. The initial attempt.
    // 2. The first retry attempt. This will fail on the invalid statement as it is aborted.
    // 3. the second retry attempt. This will succeed.
    assertThat(mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)).isEqualTo(6);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Statement(com.google.cloud.spanner.Statement) StatusRuntimeException(io.grpc.StatusRuntimeException) ResultSet(com.google.cloud.spanner.ResultSet) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 9 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testRetryUsesTags.

@Test
public void testRetryUsesTags() {
    mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_BEFORE_INSERT));
    mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, UPDATE_COUNT));
    try (ITConnection connection = createConnection()) {
        connection.setTransactionTag("transaction-tag");
        connection.setStatementTag("statement-tag");
        connection.executeUpdate(INSERT_STATEMENT);
        connection.setStatementTag("statement-tag");
        connection.executeBatchUpdate(Collections.singleton(INSERT_STATEMENT));
        connection.setStatementTag("statement-tag");
        connection.executeQuery(SELECT_COUNT_STATEMENT);
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    long executeSqlRequestCount = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).stream().filter(request -> request.getRequestOptions().getRequestTag().equals("statement-tag") && request.getRequestOptions().getTransactionTag().equals("transaction-tag")).count();
    assertEquals(4L, executeSqlRequestCount);
    long executeBatchSqlRequestCount = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class).stream().filter(request -> request.getRequestOptions().getRequestTag().equals("statement-tag") && request.getRequestOptions().getTransactionTag().equals("transaction-tag")).count();
    assertEquals(2L, executeBatchSqlRequestCount);
    long commitRequestCount = mockSpanner.getRequestsOfType(CommitRequest.class).stream().filter(request -> request.getRequestOptions().getRequestTag().equals("") && request.getRequestOptions().getTransactionTag().equals("transaction-tag")).count();
    assertEquals(2L, commitRequestCount);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) Timestamp(com.google.cloud.Timestamp) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) CountTransactionRetryListener(com.google.cloud.spanner.connection.it.ITTransactionRetryTest.CountTransactionRetryListener) ResultSet(com.google.cloud.spanner.ResultSet) CommitRequest(com.google.spanner.v1.CommitRequest) ImmutableList(com.google.common.collect.ImmutableList) AbortInterceptor(com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Status(io.grpc.Status) ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CommitRequest(com.google.spanner.v1.CommitRequest) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Example 10 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testAbortedDuringRetryOfFailedQueryAsFirstStatement.

@Test
public void testAbortedDuringRetryOfFailedQueryAsFirstStatement() {
    final Statement invalidStatement = Statement.of("SELECT * FROM FOO");
    StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
    mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
    try (ITConnection connection = createConnection(createAbortRetryListener(2, invalidStatement, notFound))) {
        try (ResultSet rs = connection.executeQuery(invalidStatement)) {
            fail("missing expected exception");
        } catch (SpannerException e) {
            assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
        }
        connection.executeUpdate(INSERT_STATEMENT);
        // Force an abort and retry.
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
    // 6 times invalid query + 2 times INSERT.
    assertThat(mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)).isEqualTo(8);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Statement(com.google.cloud.spanner.Statement) StatusRuntimeException(io.grpc.StatusRuntimeException) ResultSet(com.google.cloud.spanner.ResultSet) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Aggregations

ITConnection (com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection)11 Test (org.junit.Test)8 SpannerException (com.google.cloud.spanner.SpannerException)7 Statement (com.google.cloud.spanner.Statement)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 ResultSet (com.google.cloud.spanner.ResultSet)4 AbortInterceptor (com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor)2 CountTransactionRetryListener (com.google.cloud.spanner.connection.it.ITTransactionRetryTest.CountTransactionRetryListener)2 Timestamp (com.google.cloud.Timestamp)1 ErrorCode (com.google.cloud.spanner.ErrorCode)1 StatementResult (com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult)1 ImmutableList (com.google.common.collect.ImmutableList)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 ByteString (com.google.protobuf.ByteString)1 CommitRequest (com.google.spanner.v1.CommitRequest)1 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)1 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)1 Status (io.grpc.Status)1 Collections (java.util.Collections)1 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)1