use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor in project java-spanner by googleapis.
the class AbortedTest method testCommitAborted.
@Test
public void testCommitAborted() {
// transaction is the most recent transaction of that session.
for (int i = 0; i < 2; i++) {
mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_BEFORE_INSERT));
mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, UPDATE_COUNT));
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
// verify that the there is no test record
try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
assertThat(rs.next(), is(true));
assertThat(rs.getLong("C"), is(equalTo(0L)));
assertThat(rs.next(), is(false));
}
// do an insert
connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test aborted')"));
// indicate that the next statement should abort
interceptor.setProbability(1.0);
interceptor.setOnlyInjectOnce(true);
// do a commit that will first abort, and then on retry will succeed
connection.commit();
mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_AFTER_INSERT));
// verify that the insert succeeded
try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
assertThat(rs.next(), is(true));
assertThat(rs.getLong("C"), is(equalTo(1L)));
assertThat(rs.next(), is(false));
}
}
}
}
Aggregations