|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | 21 | import static org.junit.jupiter.api.Assertions.assertFalse; |
22 | 22 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 25 |
|
25 | 26 | import com.google.cloud.NoCredentials; |
| 27 | +import com.google.cloud.spanner.AbortedException; |
26 | 28 | import com.google.cloud.spanner.MockSpannerServiceImpl; |
27 | 29 | import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; |
28 | 30 | import com.google.cloud.spanner.Statement; |
@@ -242,6 +244,29 @@ public void testRollbackTransaction() { |
242 | 244 | assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class)); |
243 | 245 | } |
244 | 246 |
|
| 247 | + @Test |
| 248 | + public void testAbortedTransaction() { |
| 249 | + String sql = "insert into foo (id) values (1)"; |
| 250 | + mockSpanner.putStatementResult(StatementResult.update(Statement.of(sql), 1L)); |
| 251 | + |
| 252 | + ConnectionFactory connectionFactory = createConnectionFactory(); |
| 253 | + Publisher<? extends Connection> connectionPublisher = connectionFactory.create(); |
| 254 | + Connection connection = Mono.from(connectionPublisher).block(); |
| 255 | + |
| 256 | + mockSpanner.abortNextTransaction(); |
| 257 | + assertThrows(AbortedException.class, () -> Flux.concat( |
| 258 | + connection.beginTransaction(), |
| 259 | + connection.createStatement(sql).execute(), |
| 260 | + connection.commitTransaction() |
| 261 | + ).blockLast()); |
| 262 | + |
| 263 | + Mono.from(((Closeable) connectionFactory).close()).subscribe(); |
| 264 | + |
| 265 | + assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); |
| 266 | + assertEquals(0, mockSpanner.countRequestsOfType(RollbackRequest.class)); |
| 267 | + assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class)); |
| 268 | + } |
| 269 | + |
245 | 270 | @Test |
246 | 271 | public void testRollbackEmptyTransactionAndExecuteSqlInParallel() throws Exception { |
247 | 272 | // Redirect log to /dev/null. |
|
0 commit comments