Base for tests with retries on abort · googleapis/python-spanner@9903cac · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 9903cac

Browse files
committed
Base for tests with retries on abort
1 parent caa60c2 commit 9903cac

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

google/cloud/spanner_v1/testing/mock_spanner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __create_transaction(
187187
def Commit(self, request, context):
188188
self._requests.append(request)
189189
self.mock_spanner.pop_error(context)
190-
tx = self.transactions[request.transaction_id]
190+
tx = self.transactions.get(request.transaction_id, None)
191191
if tx is None:
192192
raise ValueError(f"Transaction not found: {request.transaction_id}")
193193
del self.transactions[request.transaction_id]

google/cloud/spanner_v1/transaction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def rollback(self):
197197
database._route_to_leader_enabled
198198
)
199199
)
200+
all_metadata = database.metadata_with_request_id(database._next_nth_request, 1, metadata)
200201
observability_options = getattr(database, "observability_options", None)
201202
with trace_call(
202203
"CloudSpanner.Rollback",
@@ -207,7 +208,7 @@ def rollback(self):
207208
api.rollback,
208209
session=self._session.name,
209210
transaction_id=self._transaction_id,
210-
metadata=metadata,
211+
metadata=all_metadata,
211212
)
212213
_retry(
213214
method,
@@ -288,7 +289,7 @@ def commit(
288289
method = functools.partial(
289290
api.commit,
290291
request=request,
291-
metadata=metadata,
292+
metadata=database.metadata_with_request_id(database._next_nth_request, 1, metadata),
292293
)
293294

294295
def beforeNextRetry(nthRetry, delayInSeconds):

tests/unit/test_request_id_header.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
BatchCreateSessionsRequest,
2525
ExecuteSqlRequest,
2626
)
27+
from google.api_core.exceptions import Aborted
28+
from google.rpc import code_pb2
2729
from google.cloud.spanner_v1.request_id_header import REQ_RAND_PROCESS_ID
2830

2931

@@ -193,6 +195,45 @@ def select1():
193195
]
194196
assert got_stream_segments == want_stream_segments
195197

198+
def test_retries_on_abort(self):
199+
counters = dict(aborted=0)
200+
want_failed_attempts = 2
201+
202+
def select_in_txn(txn):
203+
results = txn.execute_sql("select 1")
204+
for row in results:
205+
_ = row
206+
207+
if counters["aborted"] < want_failed_attempts:
208+
counters["aborted"] += 1
209+
raise Aborted(
210+
"Thrown from ClientInterceptor for testing",
211+
errors=[FauxCall(code_pb2.ABORTED)],
212+
)
213+
214+
add_select1_result()
215+
if not getattr(self.database, "_interceptors", None):
216+
self.database._interceptors = MockServerTestBase._interceptors
217+
218+
self.database.run_in_transaction(select_in_txn)
219+
196220
def canonicalize_request_id_headers(self):
197221
src = self.database._x_goog_request_id_interceptor
198222
return src._stream_req_segments, src._unary_req_segments
223+
224+
class FauxCall:
225+
def __init__(self, code, details="FauxCall"):
226+
self._code = code
227+
self._details = details
228+
229+
def initial_metadata(self):
230+
return {}
231+
232+
def trailing_metadata(self):
233+
return {}
234+
235+
def code(self):
236+
return self._code
237+
238+
def details(self):
239+
return self._details

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.