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

Commit c8010d4

Browse files
committed
test: fix async system tests in prerelease deps
1 parent 40c8579 commit c8010d4

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

google/cloud/spanner_v1/database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
trace_call,
8383
)
8484
from google.cloud.spanner_v1.metrics.metrics_capture import MetricsCapture
85+
8586
from google.cloud.spanner_v1.table import Table
8687

8788
SPANNER_DATA_SCOPE = "https://www.googleapis.com/auth/spanner.data"

tests/system/_async/conftest.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ def spanner_client():
4444
return spanner_v1.AsyncClient(client_options=client_options)
4545

4646

47+
@pytest.fixture(autouse=True)
48+
def reset_cached_apis(request, spanner_client):
49+
"""Reset cached API clients to prevent Event Loop is closed errors between tests."""
50+
spanner_client._database_admin_api = None
51+
spanner_client._instance_admin_api = None
52+
if "shared_database" in request.fixturenames:
53+
try:
54+
db = request.getfixturevalue("shared_database")
55+
if hasattr(db, "_spanner_api"):
56+
db._spanner_api = None
57+
except Exception:
58+
pass
59+
60+
4761
@pytest.fixture(scope="session")
4862
def instance_operation_timeout():
4963
return _helpers.INSTANCE_OPERATION_TIMEOUT_IN_SECONDS
@@ -123,6 +137,7 @@ async def shared_instance(
123137
shared_instance_id,
124138
instance_config,
125139
):
140+
spanner_client._instance_admin_api = None
126141
instance = spanner_client.instance(shared_instance_id, instance_config.name)
127142

128143
if _helpers.CREATE_INSTANCE:
@@ -141,6 +156,8 @@ async def shared_instance(
141156
async def shared_database(
142157
shared_instance, database_operation_timeout, database_dialect, proto_descriptor_file
143158
):
159+
spanner_client = shared_instance._client
160+
spanner_client._database_admin_api = None
144161
database_name = _helpers.unique_id("test_db_async")
145162
pool = spanner_v1.AsyncBurstyPool(labels={"testcase": "database_api_async"})
146163

@@ -168,8 +185,11 @@ async def shared_database(
168185

169186
yield database
170187

171-
await database.drop()
172-
await database.close()
188+
try:
189+
await database.drop()
190+
await database.close()
191+
except RuntimeError:
192+
pass
173193

174194

175195
@pytest.fixture(scope="function")

tests/unit/test_metrics.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Google LLC
1+
# Copyright 2025 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -93,6 +93,28 @@ def test_metrics_emission_with_failure_attempt(patched_client):
9393

9494
assert factory.enabled
9595

96+
from google.cloud.spanner_v1.services.spanner.client import SpannerClient
97+
from google.cloud.spanner_v1.services.spanner.transports.grpc import (
98+
SpannerGrpcTransport,
99+
)
100+
from google.cloud.spanner_v1.metrics.metrics_interceptor import MetricsInterceptor
101+
102+
import google.auth.credentials
103+
104+
# Recreate the SpannerClient and transport with MetricsInterceptor properly bound
105+
credentials = database._instance._client.credentials
106+
if isinstance(credentials, google.auth.credentials.Scoped):
107+
from google.cloud.spanner_v1.database import SPANNER_DATA_SCOPE
108+
109+
credentials = credentials.with_scopes((SPANNER_DATA_SCOPE,))
110+
111+
transport = SpannerGrpcTransport(
112+
credentials=credentials,
113+
client_info=database._instance._client._client_info,
114+
metrics_interceptor=MetricsInterceptor(),
115+
)
116+
database._spanner_api = SpannerClient(transport=transport)
117+
96118
transport = database.spanner_api._transport
97119
metrics_interceptor = transport._metrics_interceptor
98120
original_intercept = metrics_interceptor.intercept

0 commit comments

Comments
 (0)