3333from google .cloud ._helpers import UTC , _datetime_to_pb_timestamp
3434from google .cloud .spanner_v1 ._helpers import _delay_until_retry
3535from google .cloud .spanner_v1 .transaction import Transaction
36- from tests ._builders import build_spanner_api , build_session , build_transaction_pb
36+ from tests ._builders import (
37+ build_spanner_api ,
38+ build_session ,
39+ build_transaction_pb ,
40+ build_commit_response_pb ,
41+ )
3742from tests ._helpers import (
3843 OpenTelemetryBase ,
3944 LIB_VERSION ,
@@ -1051,6 +1056,41 @@ def unit_of_work(txn, *args, **kw):
10511056 def test_run_in_transaction_retry_callback_raises_abort (self ):
10521057 session = build_session ()
10531058 database = session ._database
1059+
1060+ # Build API responses.
1061+ api = database .spanner_api
1062+ begin_transaction = api .begin_transaction
1063+ streaming_read = api .streaming_read
1064+ streaming_read .side_effect = [_make_rpc_error (Aborted ), []]
1065+
1066+ # Run in transaction.
1067+ def unit_of_work (transaction ):
1068+ transaction .begin ()
1069+ list (transaction .read (TABLE_NAME , COLUMNS , KEYSET ))
1070+
1071+ session .create ()
1072+ session .run_in_transaction (unit_of_work )
1073+
1074+ self .assertEqual (begin_transaction .call_count , 2 )
1075+
1076+ begin_transaction .assert_called_with (
1077+ request = BeginTransactionRequest (
1078+ session = session .name ,
1079+ options = TransactionOptions (read_write = TransactionOptions .ReadWrite ()),
1080+ ),
1081+ metadata = [
1082+ ("google-cloud-resource-prefix" , database .name ),
1083+ ("x-goog-spanner-route-to-leader" , "true" ),
1084+ (
1085+ "x-goog-spanner-request-id" ,
1086+ f"1.{ REQ_RAND_PROCESS_ID } .{ database ._nth_client_id } .{ database ._channel_id } .4.1" ,
1087+ ),
1088+ ],
1089+ )
1090+
1091+ def test_run_in_transaction_retry_callback_raises_abort_multiplexed (self ):
1092+ session = build_session (is_multiplexed = True )
1093+ database = session ._database
10541094 api = database .spanner_api
10551095
10561096 # Build API responses
@@ -1093,6 +1133,51 @@ def unit_of_work(transaction):
10931133 ],
10941134 )
10951135
1136+ def test_run_in_transaction_retry_commit_raises_abort_multiplexed (self ):
1137+ session = build_session (is_multiplexed = True )
1138+ database = session ._database
1139+
1140+ # Build API responses
1141+ api = database .spanner_api
1142+ previous_transaction_id = b"transaction-id"
1143+ begin_transaction = api .begin_transaction
1144+ begin_transaction .return_value = build_transaction_pb (
1145+ id = previous_transaction_id
1146+ )
1147+
1148+ commit = api .commit
1149+ commit .side_effect = [_make_rpc_error (Aborted ), build_commit_response_pb ()]
1150+
1151+ # Run in transaction.
1152+ def unit_of_work (transaction ):
1153+ transaction .begin ()
1154+ list (transaction .read (TABLE_NAME , COLUMNS , KEYSET ))
1155+
1156+ session .create ()
1157+ session .run_in_transaction (unit_of_work )
1158+
1159+ # Verify retried BeginTransaction API call.
1160+ self .assertEqual (begin_transaction .call_count , 2 )
1161+
1162+ begin_transaction .assert_called_with (
1163+ request = BeginTransactionRequest (
1164+ session = session .name ,
1165+ options = TransactionOptions (
1166+ read_write = TransactionOptions .ReadWrite (
1167+ multiplexed_session_previous_transaction_id = previous_transaction_id
1168+ )
1169+ ),
1170+ ),
1171+ metadata = [
1172+ ("google-cloud-resource-prefix" , database .name ),
1173+ ("x-goog-spanner-route-to-leader" , "true" ),
1174+ (
1175+ "x-goog-spanner-request-id" ,
1176+ f"1.{ REQ_RAND_PROCESS_ID } .{ database ._nth_client_id } .{ database ._channel_id } .5.1" ,
1177+ ),
1178+ ],
1179+ )
1180+
10961181 def test_run_in_transaction_w_args_w_kwargs_wo_abort (self ):
10971182 VALUES = [
10981183 ["phred@exammple.com" , "Phred" , "Phlyntstone" , 32 ],
@@ -1279,9 +1364,7 @@ def unit_of_work(txn, *args, **kw):
12791364 request = BeginTransactionRequest (
12801365 session = session .name ,
12811366 options = TransactionOptions (
1282- read_write = TransactionOptions .ReadWrite (
1283- multiplexed_session_previous_transaction_id = TRANSACTION_ID
1284- )
1367+ read_write = TransactionOptions .ReadWrite ()
12851368 ),
12861369 ),
12871370 metadata = [
@@ -1395,9 +1478,7 @@ def unit_of_work(txn, *args, **kw):
13951478 request = BeginTransactionRequest (
13961479 session = session .name ,
13971480 options = TransactionOptions (
1398- read_write = TransactionOptions .ReadWrite (
1399- multiplexed_session_previous_transaction_id = TRANSACTION_ID
1400- )
1481+ read_write = TransactionOptions .ReadWrite ()
14011482 ),
14021483 ),
14031484 metadata = [
@@ -1662,9 +1743,7 @@ def _time(_results=[1, 2, 4, 8]):
16621743 request = BeginTransactionRequest (
16631744 session = session .name ,
16641745 options = TransactionOptions (
1665- read_write = TransactionOptions .ReadWrite (
1666- multiplexed_session_previous_transaction_id = TRANSACTION_ID
1667- )
1746+ read_write = TransactionOptions .ReadWrite ()
16681747 ),
16691748 ),
16701749 metadata = [
@@ -1680,9 +1759,7 @@ def _time(_results=[1, 2, 4, 8]):
16801759 request = BeginTransactionRequest (
16811760 session = session .name ,
16821761 options = TransactionOptions (
1683- read_write = TransactionOptions .ReadWrite (
1684- multiplexed_session_previous_transaction_id = TRANSACTION_ID
1685- )
1762+ read_write = TransactionOptions .ReadWrite ()
16861763 ),
16871764 ),
16881765 metadata = [
@@ -2074,9 +2151,7 @@ def unit_of_work(txn, *args, **kw):
20742151 request = BeginTransactionRequest (
20752152 session = session .name ,
20762153 options = TransactionOptions (
2077- read_write = TransactionOptions .ReadWrite (
2078- multiplexed_session_previous_transaction_id = TRANSACTION_ID
2079- ),
2154+ read_write = TransactionOptions .ReadWrite (),
20802155 exclude_txn_from_change_streams = True ,
20812156 ),
20822157 ),
0 commit comments