|
| 1 | +package com.google.cloud.spanner; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.ClassRule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.experimental.categories.Category; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.junit.runners.JUnit4; |
| 12 | + |
| 13 | +@Category(ParallelIntegrationTest.class) |
| 14 | +@RunWith(JUnit4.class) |
| 15 | +public class ITTransactionRetryTest { |
| 16 | + |
| 17 | + @ClassRule |
| 18 | + public static IntegrationTestEnv env = new IntegrationTestEnv(); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void TestRetryInfo() { |
| 22 | + // Creating a database with the table which contains INT64 columns |
| 23 | + Database db = env.getTestHelper().createTestDatabase("CREATE TABLE Test(ID INT64, " |
| 24 | + + "EMPID INT64) PRIMARY KEY (ID)"); |
| 25 | + DatabaseClient databaseClient = env.getTestHelper().getClient().getDatabaseClient(db.getId()); |
| 26 | + |
| 27 | + // Inserting one row |
| 28 | + databaseClient.readWriteTransaction().run(transaction -> { |
| 29 | + transaction.buffer(Mutation.newInsertBuilder("Test") |
| 30 | + .set("ID") |
| 31 | + .to(1) |
| 32 | + .set("EMPID") |
| 33 | + .to(1) |
| 34 | + .build()); |
| 35 | + return null; |
| 36 | + }); |
| 37 | + |
| 38 | + try(TransactionManager transactionManager1 = databaseClient.transactionManager()) { |
| 39 | + try(TransactionManager transactionManager2 = databaseClient.transactionManager()) { |
| 40 | + TransactionContext transaction1 = transactionManager1.begin(); |
| 41 | + TransactionContext transaction2 = transactionManager2.begin(); |
| 42 | + transaction1.executeUpdate(Statement.of("UPDATE Test SET EMPID = EMPID + 1 WHERE ID = 1")); |
| 43 | + transaction2.executeUpdate(Statement.of("UPDATE Test SET EMPID = EMPID + 1 WHERE ID = 1")); |
| 44 | + transactionManager1.commit(); |
| 45 | + AbortedException abortedException = Assert.assertThrows(AbortedException.class, |
| 46 | + transactionManager2::commit); |
| 47 | + assertThat(abortedException.getErrorCode()).isEqualTo(ErrorCode.ABORTED); |
| 48 | + assertTrue(abortedException.getRetryDelayInMillis() > 0); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments