Skip to content

Commit 41673d7

Browse files
authored
chore: unflake AsyncTransactionManagerTests (#3259)
Fixes #3240
1 parent 1eb5a31 commit 41673d7

File tree

1 file changed

+56
-95
lines changed

1 file changed

+56
-95
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncTransactionManagerTest.java

Lines changed: 56 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -346,33 +346,22 @@ public void asyncTransactionManagerFireAndForgetInvalidUpdate() throws Exception
346346
}
347347
}
348348
}
349+
ImmutableList<Class<? extends GeneratedMessageV3>> expectedRequests =
350+
ImmutableList.of(
351+
BatchCreateSessionsRequest.class,
352+
// The first update that fails. This will cause a transaction retry.
353+
ExecuteSqlRequest.class,
354+
// The retry will use an explicit BeginTransaction call.
355+
BeginTransactionRequest.class,
356+
// The first update will again fail, but now there is a transaction id, so the
357+
// transaction can continue.
358+
ExecuteSqlRequest.class,
359+
ExecuteSqlRequest.class,
360+
CommitRequest.class);
349361
if (isMultiplexedSessionsEnabled()) {
350-
assertThat(mockSpanner.getRequestTypes())
351-
.containsExactly(
352-
CreateSessionRequest.class,
353-
BatchCreateSessionsRequest.class,
354-
// The first update that fails. This will cause a transaction retry.
355-
ExecuteSqlRequest.class,
356-
// The retry will use an explicit BeginTransaction call.
357-
BeginTransactionRequest.class,
358-
// The first update will again fail, but now there is a transaction id, so the
359-
// transaction can continue.
360-
ExecuteSqlRequest.class,
361-
ExecuteSqlRequest.class,
362-
CommitRequest.class);
362+
assertThat(mockSpanner.getRequestTypes()).containsAtLeastElementsIn(expectedRequests);
363363
} else {
364-
assertThat(mockSpanner.getRequestTypes())
365-
.containsExactly(
366-
BatchCreateSessionsRequest.class,
367-
// The first update that fails. This will cause a transaction retry.
368-
ExecuteSqlRequest.class,
369-
// The retry will use an explicit BeginTransaction call.
370-
BeginTransactionRequest.class,
371-
// The first update will again fail, but now there is a transaction id, so the
372-
// transaction can continue.
373-
ExecuteSqlRequest.class,
374-
ExecuteSqlRequest.class,
375-
CommitRequest.class);
364+
assertThat(mockSpanner.getRequestTypes()).containsExactlyElementsIn(expectedRequests);
376365
}
377366
}
378367

@@ -682,21 +671,16 @@ public void asyncTransactionManagerFireAndForgetInvalidBatchUpdate() throws Exce
682671
}
683672
}
684673
}
674+
ImmutableList<Class<? extends GeneratedMessageV3>> expectedRequests =
675+
ImmutableList.of(
676+
BatchCreateSessionsRequest.class,
677+
ExecuteBatchDmlRequest.class,
678+
ExecuteBatchDmlRequest.class,
679+
CommitRequest.class);
685680
if (isMultiplexedSessionsEnabled()) {
686-
assertThat(mockSpanner.getRequestTypes())
687-
.containsExactly(
688-
CreateSessionRequest.class,
689-
BatchCreateSessionsRequest.class,
690-
ExecuteBatchDmlRequest.class,
691-
ExecuteBatchDmlRequest.class,
692-
CommitRequest.class);
681+
assertThat(mockSpanner.getRequestTypes()).containsAtLeastElementsIn(expectedRequests);
693682
} else {
694-
assertThat(mockSpanner.getRequestTypes())
695-
.containsExactly(
696-
BatchCreateSessionsRequest.class,
697-
ExecuteBatchDmlRequest.class,
698-
ExecuteBatchDmlRequest.class,
699-
CommitRequest.class);
683+
assertThat(mockSpanner.getRequestTypes()).containsExactlyElementsIn(expectedRequests);
700684
}
701685
}
702686

@@ -730,23 +714,17 @@ public void asyncTransactionManagerBatchUpdateAborted() throws Exception {
730714
assertThat(attempt.get()).isEqualTo(2);
731715
// There should only be 1 CommitRequest, as the first attempt should abort already after the
732716
// ExecuteBatchDmlRequest.
717+
ImmutableList<Class<? extends GeneratedMessageV3>> expectedRequests =
718+
ImmutableList.of(
719+
BatchCreateSessionsRequest.class,
720+
ExecuteBatchDmlRequest.class,
721+
BeginTransactionRequest.class,
722+
ExecuteBatchDmlRequest.class,
723+
CommitRequest.class);
733724
if (isMultiplexedSessionsEnabled()) {
734-
assertThat(mockSpanner.getRequestTypes())
735-
.containsExactly(
736-
CreateSessionRequest.class,
737-
BatchCreateSessionsRequest.class,
738-
ExecuteBatchDmlRequest.class,
739-
BeginTransactionRequest.class,
740-
ExecuteBatchDmlRequest.class,
741-
CommitRequest.class);
725+
assertThat(mockSpanner.getRequestTypes()).containsAtLeastElementsIn(expectedRequests);
742726
} else {
743-
assertThat(mockSpanner.getRequestTypes())
744-
.containsExactly(
745-
BatchCreateSessionsRequest.class,
746-
ExecuteBatchDmlRequest.class,
747-
BeginTransactionRequest.class,
748-
ExecuteBatchDmlRequest.class,
749-
CommitRequest.class);
727+
assertThat(mockSpanner.getRequestTypes()).containsExactlyElementsIn(expectedRequests);
750728
}
751729
}
752730

@@ -778,23 +756,17 @@ public void asyncTransactionManagerBatchUpdateAbortedBeforeFirstStatement() thro
778756
assertThat(attempt.get()).isEqualTo(2);
779757
// There should only be 1 CommitRequest, as the first attempt should abort already after the
780758
// ExecuteBatchDmlRequest.
759+
ImmutableList<Class<? extends GeneratedMessageV3>> expectedRequests =
760+
ImmutableList.of(
761+
BatchCreateSessionsRequest.class,
762+
ExecuteBatchDmlRequest.class,
763+
BeginTransactionRequest.class,
764+
ExecuteBatchDmlRequest.class,
765+
CommitRequest.class);
781766
if (isMultiplexedSessionsEnabled()) {
782-
assertThat(mockSpanner.getRequestTypes())
783-
.containsExactly(
784-
CreateSessionRequest.class,
785-
BatchCreateSessionsRequest.class,
786-
ExecuteBatchDmlRequest.class,
787-
BeginTransactionRequest.class,
788-
ExecuteBatchDmlRequest.class,
789-
CommitRequest.class);
767+
assertThat(mockSpanner.getRequestTypes()).containsAtLeastElementsIn(expectedRequests);
790768
} else {
791-
assertThat(mockSpanner.getRequestTypes())
792-
.containsExactly(
793-
BatchCreateSessionsRequest.class,
794-
ExecuteBatchDmlRequest.class,
795-
BeginTransactionRequest.class,
796-
ExecuteBatchDmlRequest.class,
797-
CommitRequest.class);
769+
assertThat(mockSpanner.getRequestTypes()).containsExactlyElementsIn(expectedRequests);
798770
}
799771
}
800772

@@ -844,25 +816,18 @@ public void asyncTransactionManagerWithBatchUpdateCommitAborted() throws Excepti
844816
} finally {
845817
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
846818
}
819+
ImmutableList<Class<? extends GeneratedMessageV3>> expectedRequests =
820+
ImmutableList.of(
821+
BatchCreateSessionsRequest.class,
822+
ExecuteBatchDmlRequest.class,
823+
CommitRequest.class,
824+
BeginTransactionRequest.class,
825+
ExecuteBatchDmlRequest.class,
826+
CommitRequest.class);
847827
if (isMultiplexedSessionsEnabled()) {
848-
assertThat(mockSpanner.getRequestTypes())
849-
.containsExactly(
850-
CreateSessionRequest.class,
851-
BatchCreateSessionsRequest.class,
852-
ExecuteBatchDmlRequest.class,
853-
CommitRequest.class,
854-
BeginTransactionRequest.class,
855-
ExecuteBatchDmlRequest.class,
856-
CommitRequest.class);
828+
assertThat(mockSpanner.getRequestTypes()).containsAtLeastElementsIn(expectedRequests);
857829
} else {
858-
assertThat(mockSpanner.getRequestTypes())
859-
.containsExactly(
860-
BatchCreateSessionsRequest.class,
861-
ExecuteBatchDmlRequest.class,
862-
CommitRequest.class,
863-
BeginTransactionRequest.class,
864-
ExecuteBatchDmlRequest.class,
865-
CommitRequest.class);
830+
assertThat(mockSpanner.getRequestTypes()).containsExactlyElementsIn(expectedRequests);
866831
}
867832
}
868833

@@ -926,7 +891,7 @@ public void asyncTransactionManagerBatchUpdateAbortedWithoutGettingResult() thro
926891
}
927892

928893
@Test
929-
public void asyncTransactionManagerWithBatchUpdateCommitFails() throws Exception {
894+
public void asyncTransactionManagerWithBatchUpdateCommitFails() {
930895
mockSpanner.setCommitExecutionTime(
931896
SimulatedExecutionTime.ofException(
932897
Status.INVALID_ARGUMENT
@@ -949,17 +914,13 @@ public void asyncTransactionManagerWithBatchUpdateCommitFails() throws Exception
949914
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
950915
assertThat(e.getMessage()).contains("mutation limit exceeded");
951916
}
917+
ImmutableList<Class<? extends GeneratedMessageV3>> expectedRequests =
918+
ImmutableList.of(
919+
BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
952920
if (isMultiplexedSessionsEnabled()) {
953-
assertThat(mockSpanner.getRequestTypes())
954-
.containsExactly(
955-
CreateSessionRequest.class,
956-
BatchCreateSessionsRequest.class,
957-
ExecuteBatchDmlRequest.class,
958-
CommitRequest.class);
921+
assertThat(mockSpanner.getRequestTypes()).containsAtLeastElementsIn(expectedRequests);
959922
} else {
960-
assertThat(mockSpanner.getRequestTypes())
961-
.containsExactly(
962-
BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
923+
assertThat(mockSpanner.getRequestTypes()).containsExactlyElementsIn(expectedRequests);
963924
}
964925
}
965926

0 commit comments

Comments
 (0)