@@ -255,6 +255,130 @@ public Long run(TransactionContext transaction) throws Exception {
|
255 | 255 | assertThat(countTransactionsStarted()).isEqualTo(2);
|
256 | 256 | }
|
257 | 257 |
|
| 258 | +@Test |
| 259 | +public void testInlinedBeginFirstUpdateAborts() { |
| 260 | +DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 261 | +long updateCount = |
| 262 | +client |
| 263 | +.readWriteTransaction() |
| 264 | +.run( |
| 265 | +new TransactionCallable<Long>() { |
| 266 | +boolean firstAttempt = true; |
| 267 | + |
| 268 | +@Override |
| 269 | +public Long run(TransactionContext transaction) throws Exception { |
| 270 | +if (firstAttempt) { |
| 271 | +firstAttempt = false; |
| 272 | +mockSpanner.putStatementResult( |
| 273 | +StatementResult.exception( |
| 274 | +UPDATE_STATEMENT, |
| 275 | +mockSpanner.createAbortedException( |
| 276 | +ByteString.copyFromUtf8("some-tx")))); |
| 277 | +} else { |
| 278 | +mockSpanner.putStatementResult( |
| 279 | +StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT)); |
| 280 | +} |
| 281 | +return transaction.executeUpdate(UPDATE_STATEMENT); |
| 282 | +} |
| 283 | +}); |
| 284 | +assertThat(updateCount).isEqualTo(UPDATE_COUNT); |
| 285 | +assertThat(countRequests(BeginTransactionRequest.class)).isEqualTo(1); |
| 286 | +assertThat(countRequests(ExecuteSqlRequest.class)).isEqualTo(2); |
| 287 | +assertThat(countRequests(CommitRequest.class)).isEqualTo(1); |
| 288 | +} |
| 289 | + |
| 290 | +@Test |
| 291 | +public void testInlinedBeginFirstQueryAborts() { |
| 292 | +DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 293 | +long updateCount = |
| 294 | +client |
| 295 | +.readWriteTransaction() |
| 296 | +.run( |
| 297 | +new TransactionCallable<Long>() { |
| 298 | +boolean firstAttempt = true; |
| 299 | + |
| 300 | +@Override |
| 301 | +public Long run(TransactionContext transaction) throws Exception { |
| 302 | +if (firstAttempt) { |
| 303 | +firstAttempt = false; |
| 304 | +mockSpanner.putStatementResult( |
| 305 | +StatementResult.exception( |
| 306 | +SELECT1, |
| 307 | +mockSpanner.createAbortedException( |
| 308 | +ByteString.copyFromUtf8("some-tx")))); |
| 309 | +} else { |
| 310 | +mockSpanner.putStatementResult( |
| 311 | +StatementResult.query(SELECT1, SELECT1_RESULTSET)); |
| 312 | +} |
| 313 | +try (ResultSet rs = transaction.executeQuery(SELECT1)) { |
| 314 | +while (rs.next()) { |
| 315 | +return rs.getLong(0); |
| 316 | +} |
| 317 | +} |
| 318 | +return 0L; |
| 319 | +} |
| 320 | +}); |
| 321 | +assertThat(updateCount).isEqualTo(1L); |
| 322 | +assertThat(countRequests(BeginTransactionRequest.class)).isEqualTo(1); |
| 323 | +assertThat(countRequests(ExecuteSqlRequest.class)).isEqualTo(2); |
| 324 | +assertThat(countRequests(CommitRequest.class)).isEqualTo(1); |
| 325 | +} |
| 326 | + |
| 327 | +@Test |
| 328 | +public void testInlinedBeginFirstQueryReturnsUnavailable() { |
| 329 | +DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 330 | +mockSpanner.setExecuteSqlExecutionTime( |
| 331 | +SimulatedExecutionTime.ofStreamException(Status.UNAVAILABLE.asRuntimeException(), 0)); |
| 332 | +long value = |
| 333 | +client |
| 334 | +.readWriteTransaction() |
| 335 | +.run( |
| 336 | +new TransactionCallable<Long>() { |
| 337 | +@Override |
| 338 | +public Long run(TransactionContext transaction) throws Exception { |
| 339 | +// The first attempt will return UNAVAILABLE and retry internally. |
| 340 | +try (ResultSet rs = transaction.executeQuery(SELECT1)) { |
| 341 | +while (rs.next()) { |
| 342 | +return rs.getLong(0); |
| 343 | +} |
| 344 | +} |
| 345 | +return 0L; |
| 346 | +} |
| 347 | +}); |
| 348 | +assertThat(value).isEqualTo(1L); |
| 349 | +assertThat(countRequests(BeginTransactionRequest.class)).isEqualTo(0); |
| 350 | +assertThat(countRequests(ExecuteSqlRequest.class)).isEqualTo(2); |
| 351 | +assertThat(countRequests(CommitRequest.class)).isEqualTo(1); |
| 352 | +} |
| 353 | + |
| 354 | +@Test |
| 355 | +public void testInlinedBeginFirstReadReturnsUnavailable() { |
| 356 | +DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 357 | +mockSpanner.setReadExecutionTime( |
| 358 | +SimulatedExecutionTime.ofStreamException(Status.UNAVAILABLE.asRuntimeException(), 0)); |
| 359 | +long value = |
| 360 | +client |
| 361 | +.readWriteTransaction() |
| 362 | +.run( |
| 363 | +new TransactionCallable<Long>() { |
| 364 | +@Override |
| 365 | +public Long run(TransactionContext transaction) throws Exception { |
| 366 | +// The first attempt will return UNAVAILABLE and retry internally. |
| 367 | +try (ResultSet rs = |
| 368 | +transaction.read("FOO", KeySet.all(), Arrays.asList("ID"))) { |
| 369 | +while (rs.next()) { |
| 370 | +return rs.getLong(0); |
| 371 | +} |
| 372 | +} |
| 373 | +return 0L; |
| 374 | +} |
| 375 | +}); |
| 376 | +assertThat(value).isEqualTo(1L); |
| 377 | +assertThat(countRequests(BeginTransactionRequest.class)).isEqualTo(0); |
| 378 | +assertThat(countRequests(ReadRequest.class)).isEqualTo(2); |
| 379 | +assertThat(countRequests(CommitRequest.class)).isEqualTo(1); |
| 380 | +} |
| 381 | + |
258 | 382 | @Test
|
259 | 383 | public void testInlinedBeginTxWithQuery() {
|
260 | 384 | DatabaseClient client =
|
@@ -283,8 +407,7 @@ public Long run(TransactionContext transaction) throws Exception {
|
283 | 407 |
|
284 | 408 | @Test
|
285 | 409 | public void testInlinedBeginTxWithRead() {
|
286 |
| -DatabaseClient client = |
287 |
| -spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); |
| 410 | +DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
288 | 411 | long updateCount =
|
289 | 412 | client
|
290 | 413 | .readWriteTransaction()
|
|
0 commit comments