1616from google .auth .credentials import AnonymousCredentials
1717from google .rpc import code_pb2
1818import pytest
19+ from unittest .mock import patch , PropertyMock
1920
2021from google .cloud .spanner_v1 import Client
22+ from google .cloud .spanner_v1 .session import Session
2123
2224from . import _helpers
2325
@@ -196,9 +198,15 @@ def create_db_trace_exporter():
196198 not HAS_OTEL_INSTALLED ,
197199 reason = "Tracing requires OpenTelemetry" ,
198200)
199- def test_transaction_abort_then_retry_spans ():
201+ @patch .object (Session , "session_id" , new_callable = PropertyMock )
202+ @patch .object (Session , "is_multiplexed" , new_callable = PropertyMock )
203+ def test_transaction_abort_then_retry_spans (mock_session_multiplexed , mock_session_id ):
200204 from opentelemetry .trace .status import StatusCode
201205
206+ # Mock session properties for testing.
207+ mock_session_multiplexed .return_value = session_multiplexed = False
208+ mock_session_id .return_value = session_id = "session-id"
209+
202210 db , trace_exporter = create_db_trace_exporter ()
203211
204212 counters = dict (aborted = 0 )
@@ -225,12 +233,14 @@ def select_in_txn(txn):
225233 ("Waiting for a session to become available" , {"kind" : "BurstyPool" }),
226234 ("No sessions available in pool. Creating session" , {"kind" : "BurstyPool" }),
227235 ("Creating Session" , {}),
236+ ("Using session" , {"id" : session_id , "multiplexed" : session_multiplexed }),
228237 (
229238 "Transaction was aborted in user operation, retrying" ,
230239 {"delay_seconds" : "EPHEMERAL" , "cause" : "EPHEMERAL" , "attempt" : 1 },
231240 ),
232241 ("Starting Commit" , {}),
233242 ("Commit Done" , {}),
243+ ("Returning session" , {"id" : session_id , "multiplexed" : session_multiplexed }),
234244 ]
235245 assert got_events == want_events
236246
@@ -392,9 +402,15 @@ def tx_update(txn):
392402 not HAS_OTEL_INSTALLED ,
393403 reason = "Tracing requires OpenTelemetry" ,
394404)
395- def test_database_partitioned_error ():
405+ @patch .object (Session , "session_id" , new_callable = PropertyMock )
406+ @patch .object (Session , "is_multiplexed" , new_callable = PropertyMock )
407+ def test_database_partitioned_error (mock_session_multiplexed , mock_session_id ):
396408 from opentelemetry .trace .status import StatusCode
397409
410+ # Mock session properties for testing.
411+ mock_session_multiplexed .return_value = session_multiplexed = False
412+ mock_session_id .return_value = session_id = "session-id"
413+
398414 db , trace_exporter = create_db_trace_exporter ()
399415
400416 try :
@@ -409,6 +425,7 @@ def test_database_partitioned_error():
409425 ("Waiting for a session to become available" , {"kind" : "BurstyPool" }),
410426 ("No sessions available in pool. Creating session" , {"kind" : "BurstyPool" }),
411427 ("Creating Session" , {}),
428+ ("Using session" , {"id" : session_id , "multiplexed" : session_multiplexed }),
412429 ("Starting BeginTransaction" , {}),
413430 (
414431 "exception" ,
@@ -428,6 +445,7 @@ def test_database_partitioned_error():
428445 "exception.escaped" : "False" ,
429446 },
430447 ),
448+ ("Returning session" , {"id" : session_id , "multiplexed" : session_multiplexed }),
431449 ]
432450 assert got_events == want_events
433451
0 commit comments