Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 9456c6f

Browse files
committed
test: fix retry helpers currently causing flaky test failures
Fix the retry helpers that are currently causing multiple tests to fail randomly.
1 parent 3943885 commit 9456c6f

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

tests/mockserver_tests/test_aborted_transaction.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import random
1415

1516
from google.cloud.spanner_v1 import (
1617
BatchCreateSessionsRequest,
@@ -29,6 +30,12 @@
2930
add_update_count,
3031
add_single_result,
3132
)
33+
from google.api_core import exceptions
34+
from test_utils import retry
35+
36+
retry_maybe_aborted_txn = retry.RetryErrors(
37+
exceptions.Aborted, max_tries=5, delay=0, backoff=1
38+
)
3239

3340

3441
class TestAbortedTransaction(MockServerTestBase):
@@ -119,6 +126,18 @@ def test_batch_commit_aborted(self):
119126
# The transaction is aborted and retried.
120127
self.assertTrue(isinstance(requests[2], CommitRequest))
121128

129+
@retry_maybe_aborted_txn
130+
def test_retry_helper(self):
131+
# Randomly add an Aborted error for the Commit method on the mock server.
132+
if random.random() < 0.5:
133+
add_error(SpannerServicer.Commit.__name__, aborted_status())
134+
session = self.database.session()
135+
session.create()
136+
transaction = session.transaction()
137+
transaction.begin()
138+
transaction.insert("my_table", ["col1, col2"], [{"col1": 1, "col2": "One"}])
139+
transaction.commit()
140+
122141

123142
def _insert_mutations(transaction: Transaction):
124143
transaction.insert("my_table", ["col1", "col2"], ["value1", "value2"])

tests/system/_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
retry_429_503 = retry.RetryErrors(
7575
exceptions.TooManyRequests, exceptions.ServiceUnavailable, 8
7676
)
77-
retry_mabye_aborted_txn = retry.RetryErrors(exceptions.ServerError, exceptions.Aborted)
78-
retry_mabye_conflict = retry.RetryErrors(exceptions.ServerError, exceptions.Conflict)
77+
retry_maybe_aborted_txn = retry.RetryErrors(exceptions.Aborted)
78+
retry_maybe_conflict = retry.RetryErrors(exceptions.Conflict)
7979

8080

8181
def _has_all_ddl(database):

tests/system/test_session_api.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def test_batch_insert_w_commit_timestamp(sessions_database, not_postgres):
578578
assert not deleted
579579

580580

581-
@_helpers.retry_mabye_aborted_txn
581+
@_helpers.retry_maybe_aborted_txn
582582
def test_transaction_read_and_insert_then_rollback(
583583
sessions_database,
584584
ot_exporter,
@@ -687,7 +687,7 @@ def test_transaction_read_and_insert_then_rollback(
687687
)
688688

689689

690-
@_helpers.retry_mabye_conflict
690+
@_helpers.retry_maybe_conflict
691691
def test_transaction_read_and_insert_then_exception(sessions_database):
692692
class CustomException(Exception):
693693
pass
@@ -714,7 +714,7 @@ def _transaction_read_then_raise(transaction):
714714
assert rows == []
715715

716716

717-
@_helpers.retry_mabye_conflict
717+
@_helpers.retry_maybe_conflict
718718
def test_transaction_read_and_insert_or_update_then_commit(
719719
sessions_database,
720720
sessions_to_delete,
@@ -771,8 +771,8 @@ def _generate_insert_returning_statement(row, database_dialect):
771771
return f"INSERT INTO {table} ({column_list}) VALUES ({row_data}) {returning}"
772772

773773

774-
@_helpers.retry_mabye_conflict
775-
@_helpers.retry_mabye_aborted_txn
774+
@_helpers.retry_maybe_conflict
775+
@_helpers.retry_maybe_aborted_txn
776776
def test_transaction_execute_sql_w_dml_read_rollback(
777777
sessions_database,
778778
sessions_to_delete,
@@ -809,7 +809,7 @@ def test_transaction_execute_sql_w_dml_read_rollback(
809809
# [END spanner_test_dml_rollback_txn_not_committed]
810810

811811

812-
@_helpers.retry_mabye_conflict
812+
@_helpers.retry_maybe_conflict
813813
def test_transaction_execute_update_read_commit(sessions_database, sessions_to_delete):
814814
# [START spanner_test_dml_read_your_writes]
815815
sd = _sample_data
@@ -838,7 +838,7 @@ def test_transaction_execute_update_read_commit(sessions_database, sessions_to_d
838838
# [END spanner_test_dml_read_your_writes]
839839

840840

841-
@_helpers.retry_mabye_conflict
841+
@_helpers.retry_maybe_conflict
842842
def test_transaction_execute_update_then_insert_commit(
843843
sessions_database, sessions_to_delete
844844
):
@@ -870,7 +870,7 @@ def test_transaction_execute_update_then_insert_commit(
870870
# [END spanner_test_dml_with_mutation]
871871

872872

873-
@_helpers.retry_mabye_conflict
873+
@_helpers.retry_maybe_conflict
874874
@pytest.mark.skipif(
875875
_helpers.USE_EMULATOR, reason="Emulator does not support DML Returning."
876876
)
@@ -901,7 +901,7 @@ def test_transaction_execute_sql_dml_returning(
901901
sd._check_rows_data(rows)
902902

903903

904-
@_helpers.retry_mabye_conflict
904+
@_helpers.retry_maybe_conflict
905905
@pytest.mark.skipif(
906906
_helpers.USE_EMULATOR, reason="Emulator does not support DML Returning."
907907
)
@@ -929,7 +929,7 @@ def test_transaction_execute_update_dml_returning(
929929
sd._check_rows_data(rows)
930930

931931

932-
@_helpers.retry_mabye_conflict
932+
@_helpers.retry_maybe_conflict
933933
@pytest.mark.skipif(
934934
_helpers.USE_EMULATOR, reason="Emulator does not support DML Returning."
935935
)

0 commit comments

Comments
 (0)