fix tests · googleapis/python-spanner@94b88dd · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 94b88dd

Browse files
committed
fix tests
1 parent 15d245f commit 94b88dd

File tree

10 files changed

+450
-114
lines changed

10 files changed

+450
-114
lines changed

.github/workflows/integration-tests-against-emulator-with-multiplexed-session.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ jobs:
3131
GOOGLE_CLOUD_PROJECT: emulator-test-project
3232
GOOGLE_CLOUD_TESTS_CREATE_SPANNER_INSTANCE: true
3333
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS: true
34+
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS: true

google/cloud/spanner_v1/database.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ def __init__(
202202
self._pool = pool
203203
pool.bind(self)
204204

205-
# Initialize session options and sessions manager for multiplexed session support
206205
self.session_options = SessionOptions()
207206
self._sessions_manager = DatabaseSessionsManager(self, pool)
208207

@@ -767,7 +766,6 @@ def execute_pdml():
767766
) as span, MetricsCapture():
768767
from google.cloud.spanner_v1.session_options import TransactionType
769768

770-
# Use sessions manager for partitioned DML operations
771769
session = self._sessions_manager.get_session(
772770
TransactionType.PARTITIONED
773771
)
@@ -1926,7 +1924,8 @@ def close(self):
19261924
from all the partitions.
19271925
"""
19281926
if self._session is not None:
1929-
self._session.delete()
1927+
if not self._session.is_multiplexed:
1928+
self._session.delete()
19301929

19311930

19321931
def _check_ddl_statements(value):

google/cloud/spanner_v1/pool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ def put(self, session):
449449
self._sessions.put_nowait(session)
450450
except queue.Full:
451451
try:
452+
# Sessions from pools are never multiplexed, so we can always delete them
452453
session.delete()
453454
except NotFound:
454455
pass

google/cloud/spanner_v1/session.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,18 @@ def create(self):
173173
if self._labels:
174174
request.session.labels = self._labels
175175

176+
# Set the multiplexed field for multiplexed sessions
177+
if self._is_multiplexed:
178+
request.session.multiplexed = True
179+
176180
observability_options = getattr(self._database, "observability_options", None)
181+
span_name = (
182+
"CloudSpanner.CreateMultiplexedSession"
183+
if self._is_multiplexed
184+
else "CloudSpanner.CreateSession"
185+
)
177186
with trace_call(
178-
"CloudSpanner.CreateSession",
187+
span_name,
179188
self,
180189
self._labels,
181190
observability_options=observability_options,

google/cloud/spanner_v1/session_options.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ def use_multiplexed(self, transaction_type: TransactionType) -> bool:
5959
* ENV_VAR_ENABLE_MULTIPLEXED is set to true;
6060
* ENV_VAR_ENABLE_MULTIPLEXED_FOR_PARTITIONED is set to true; and
6161
* multiplexed sessions have not been disabled for partitioned transactions.
62-
Multiplexed sessions are enabled for read/write transactions if:
63-
* ENV_VAR_ENABLE_MULTIPLEXED is set to true;
64-
* ENV_VAR_ENABLE_MULTIPLEXED_FOR_PARTITIONED is set to true; and
65-
* multiplexed sessions have not been disabled for read/write transactions.
62+
Multiplexed sessions are **currently disabled** for read / write.
6663
:type transaction_type: :class:`TransactionType`
6764
:param transaction_type: the type of transaction to check whether
6865
multiplexed sessions should be used.
@@ -102,6 +99,10 @@ def disable_multiplexed(
10299
disable_multiplexed_log_msg_fstring = (
103100
"Disabling multiplexed sessions for {transaction_type_value} transactions"
104101
)
102+
import logging
103+
104+
if logger is None:
105+
logger = logging.getLogger(__name__)
105106

106107
if transaction_type is None:
107108
logger.warning(

tests/system/test_observability_options.py

Lines changed: 85 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,23 @@ def test_propagation(enable_extended_tracing):
109109
len(from_inject_spans) >= 2
110110
) # "Expecting at least 2 spans from the injected trace exporter"
111111
gotNames = [span.name for span in from_inject_spans]
112+
113+
# Check if multiplexed sessions are enabled
114+
import os
115+
116+
multiplexed_enabled = (
117+
os.getenv("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS", "").lower() == "true"
118+
)
119+
120+
# Determine expected session span name based on multiplexed sessions
121+
expected_session_span_name = (
122+
"CloudSpanner.CreateMultiplexedSession"
123+
if multiplexed_enabled
124+
else "CloudSpanner.CreateSession"
125+
)
126+
112127
wantNames = [
113-
"CloudSpanner.CreateSession",
128+
expected_session_span_name,
114129
"CloudSpanner.Snapshot.execute_sql",
115130
]
116131
assert gotNames == wantNames
@@ -392,6 +407,7 @@ def tx_update(txn):
392407
reason="Tracing requires OpenTelemetry",
393408
)
394409
def test_database_partitioned_error():
410+
import os
395411
from opentelemetry.trace.status import StatusCode
396412

397413
db, trace_exporter = create_db_trace_exporter()
@@ -402,43 +418,84 @@ def test_database_partitioned_error():
402418
pass
403419

404420
got_statuses, got_events = finished_spans_statuses(trace_exporter)
405-
# Check for the series of events
406-
want_events = [
407-
("Acquiring session", {"kind": "BurstyPool"}),
408-
("Waiting for a session to become available", {"kind": "BurstyPool"}),
409-
("No sessions available in pool. Creating session", {"kind": "BurstyPool"}),
410-
("Creating Session", {}),
411-
("Starting BeginTransaction", {}),
412-
(
421+
422+
multiplexed_partitioned_enabled = (
423+
os.getenv("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS") == "true"
424+
)
425+
426+
if multiplexed_partitioned_enabled:
427+
expected_event_names = [
428+
"Creating Session",
429+
"Using session",
430+
"Starting BeginTransaction",
431+
"Returning session",
413432
"exception",
414-
{
415-
"exception.type": "google.api_core.exceptions.InvalidArgument",
416-
"exception.message": "400 Table not found: NonExistent [at 1:8]\nUPDATE NonExistent SET name = 'foo' WHERE id > 1\n ^",
417-
"exception.stacktrace": "EPHEMERAL",
418-
"exception.escaped": "False",
419-
},
420-
),
421-
(
422433
"exception",
423-
{
424-
"exception.type": "google.api_core.exceptions.InvalidArgument",
425-
"exception.message": "400 Table not found: NonExistent [at 1:8]\nUPDATE NonExistent SET name = 'foo' WHERE id > 1\n ^",
426-
"exception.stacktrace": "EPHEMERAL",
427-
"exception.escaped": "False",
428-
},
429-
),
430-
]
431-
assert got_events == want_events
434+
]
435+
assert len(got_events) == len(expected_event_names)
436+
for i, expected_name in enumerate(expected_event_names):
437+
assert got_events[i][0] == expected_name
438+
439+
assert got_events[1][1]["multiplexed"] is True
440+
441+
assert got_events[3][1]["multiplexed"] is True
442+
443+
for i in [4, 5]:
444+
assert (
445+
got_events[i][1]["exception.type"]
446+
== "google.api_core.exceptions.InvalidArgument"
447+
)
448+
assert (
449+
"Table not found: NonExistent" in got_events[i][1]["exception.message"]
450+
)
451+
else:
452+
expected_event_names = [
453+
"Acquiring session",
454+
"Waiting for a session to become available",
455+
"No sessions available in pool. Creating session",
456+
"Creating Session",
457+
"Using session",
458+
"Starting BeginTransaction",
459+
"Returning session",
460+
"exception",
461+
"exception",
462+
]
463+
464+
assert len(got_events) == len(expected_event_names)
465+
for i, expected_name in enumerate(expected_event_names):
466+
assert got_events[i][0] == expected_name
467+
468+
assert got_events[0][1]["kind"] == "BurstyPool"
469+
assert got_events[1][1]["kind"] == "BurstyPool"
470+
assert got_events[2][1]["kind"] == "BurstyPool"
471+
472+
assert got_events[4][1]["multiplexed"] is False
473+
474+
assert got_events[6][1]["multiplexed"] is False
475+
476+
for i in [7, 8]:
477+
assert (
478+
got_events[i][1]["exception.type"]
479+
== "google.api_core.exceptions.InvalidArgument"
480+
)
481+
assert (
482+
"Table not found: NonExistent" in got_events[i][1]["exception.message"]
483+
)
432484

433-
# Check for the statues.
434485
codes = StatusCode
486+
487+
expected_session_span_name = (
488+
"CloudSpanner.CreateMultiplexedSession"
489+
if multiplexed_partitioned_enabled
490+
else "CloudSpanner.CreateSession"
491+
)
435492
want_statuses = [
436493
(
437494
"CloudSpanner.Database.execute_partitioned_pdml",
438495
codes.ERROR,
439496
"InvalidArgument: 400 Table not found: NonExistent [at 1:8]\nUPDATE NonExistent SET name = 'foo' WHERE id > 1\n ^",
440497
),
441-
("CloudSpanner.CreateSession", codes.OK, None),
498+
(expected_session_span_name, codes.OK, None),
442499
(
443500
"CloudSpanner.ExecuteStreamingSql",
444501
codes.ERROR,

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.