|
18 | 18 |
|
19 | 19 | import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator; |
20 | 20 | import static com.google.common.truth.Truth.assertThat; |
| 21 | +import static org.junit.Assert.assertFalse; |
21 | 22 | import static org.junit.Assert.assertTrue; |
22 | 23 | import static org.junit.Assume.assumeFalse; |
| 24 | +import static org.junit.Assume.assumeTrue; |
23 | 25 |
|
24 | 26 | import org.junit.ClassRule; |
25 | 27 | import org.junit.Test; |
@@ -84,4 +86,56 @@ public void TestRetryInfo() { |
84 | 86 |
|
85 | 87 | assertTrue("Transaction is not aborted with the trailers", isAbortedWithRetryInfo); |
86 | 88 | } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void TestRetryInfoWithDirectPath() { |
| 92 | + assumeFalse("emulator does not support parallel transaction", isUsingEmulator()); |
| 93 | + // TODO(sakthivelmani) - Re-enable once b/422916293 is resolved |
| 94 | + assumeTrue( |
| 95 | + "Enabling this test due to bug b/422916293", |
| 96 | + env.getTestHelper().getOptions().isEnableDirectAccess()); |
| 97 | + |
| 98 | + // Creating a database with the table which contains INT64 columns |
| 99 | + Database db = |
| 100 | + env.getTestHelper() |
| 101 | + .createTestDatabase("CREATE TABLE Test(ID INT64, " + "EMPID INT64) PRIMARY KEY (ID)"); |
| 102 | + DatabaseClient databaseClient = env.getTestHelper().getClient().getDatabaseClient(db.getId()); |
| 103 | + |
| 104 | + // Inserting one row |
| 105 | + databaseClient |
| 106 | + .readWriteTransaction() |
| 107 | + .run( |
| 108 | + transaction -> { |
| 109 | + transaction.buffer( |
| 110 | + Mutation.newInsertBuilder("Test").set("ID").to(1).set("EMPID").to(1).build()); |
| 111 | + return null; |
| 112 | + }); |
| 113 | + |
| 114 | + int numRetries = 10; |
| 115 | + boolean isAbortedWithRetryInfo = false; |
| 116 | + while (numRetries-- > 0) { |
| 117 | + try (TransactionManager transactionManager1 = databaseClient.transactionManager()) { |
| 118 | + try (TransactionManager transactionManager2 = databaseClient.transactionManager()) { |
| 119 | + try { |
| 120 | + TransactionContext transaction1 = transactionManager1.begin(); |
| 121 | + TransactionContext transaction2 = transactionManager2.begin(); |
| 122 | + transaction1.executeUpdate( |
| 123 | + Statement.of("UPDATE Test SET EMPID = EMPID + 1 WHERE ID = 1")); |
| 124 | + transaction2.executeUpdate( |
| 125 | + Statement.of("UPDATE Test SET EMPID = EMPID + 1 WHERE ID = 1")); |
| 126 | + transactionManager1.commit(); |
| 127 | + transactionManager2.commit(); |
| 128 | + } catch (AbortedException abortedException) { |
| 129 | + assertThat(abortedException.getErrorCode()).isEqualTo(ErrorCode.ABORTED); |
| 130 | + if (abortedException.getRetryDelayInMillis() > 0) { |
| 131 | + isAbortedWithRetryInfo = true; |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + assertFalse("Transaction is aborted with the trailers", isAbortedWithRetryInfo); |
| 140 | + } |
87 | 141 | } |
0 commit comments