Skip to content

Commit 805dcd1

Browse files
test(spanner): upgrade Spanner sample instance fixtures to session scope
Previously, standard and multi-region Spanner instance fixtures in conftest.py were module-scoped, meaning a brand-new Spanner instance was created and destroyed for every single test file, adding massive execution time (~2 minutes per file) in CI/CD runs. This change upgrades the instance and config fixtures to session scope. To prevent ScopeMismatch exceptions, the dependencies on module-scoped sample_name are replaced with a generic shared-samples label. This allows the test suite to reuse a single shared Spanner instance, dramatically optimizing test execution times.
1 parent e77dc55 commit 805dcd1

11 files changed

Lines changed: 10 additions & 75 deletions

packages/google-cloud-spanner/samples/samples/archived/backup_snippet_test.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
from google.api_core.exceptions import DeadlineExceeded
1919
from test_utils.retry import RetryErrors
2020

21-
22-
@pytest.fixture(scope="module")
23-
def sample_name():
24-
return "backup"
25-
26-
2721
def unique_database_id():
2822
"""Creates a unique id for the database."""
2923
return f"test-db-{uuid.uuid4().hex[:10]}"

packages/google-cloud-spanner/samples/samples/archived/pg_samples_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@
4545
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)
4646

4747

48-
@pytest.fixture(scope="module")
49-
def sample_name():
50-
return "pg_snippets"
51-
52-
5348
@pytest.fixture(scope="module")
5449
def database_dialect():
5550
"""Spanner dialect to be used for this sample.

packages/google-cloud-spanner/samples/samples/archived/samples_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@
5353
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)
5454

5555

56-
@pytest.fixture(scope="module")
57-
def sample_name():
58-
return "snippets"
59-
60-
6156
@pytest.fixture(scope="module")
6257
def database_dialect():
6358
"""Spanner dialect to be used for this sample.

packages/google-cloud-spanner/samples/samples/autocommit_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
import autocommit
1212

1313

14-
@pytest.fixture(scope="module")
15-
def sample_name():
16-
return "autocommit"
17-
18-
1914
@RetryErrors(exception=Aborted, max_tries=2)
2015
def test_enable_autocommit_mode(capsys, instance_id, sample_database):
2116
# Delete table if it exists for retry attempts.

packages/google-cloud-spanner/samples/samples/backup_sample_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
import backup_sample
2121

2222

23-
@pytest.fixture(scope="module")
24-
def sample_name():
25-
return "backup"
26-
27-
2823
def unique_database_id():
2924
"""Creates a unique id for the database."""
3025
return f"test-db-{uuid.uuid4().hex[:10]}"

packages/google-cloud-spanner/samples/samples/backup_schedule_samples_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
__INCREMENTAL_BACKUP_SCHEDULE_ID = "incremental-backup-schedule"
2323

2424

25-
@pytest.fixture(scope="module")
26-
def sample_name():
27-
return "backup_schedule"
28-
29-
3025
@pytest.fixture(scope="module")
3126
def database_id():
3227
return f"test-db-{uuid.uuid4().hex[:10]}"

packages/google-cloud-spanner/samples/samples/conftest.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,6 @@
3333
(exceptions.ResourceExhausted, exceptions.FailedPrecondition), max_tries=6, delay=15
3434
)
3535

36-
37-
@pytest.fixture(scope="module")
38-
def sample_name():
39-
"""Sample testcase modules must define this fixture.
40-
41-
The name is used to label the instance created by the sample, to
42-
aid in debugging leaked instances.
43-
"""
44-
raise NotImplementedError("Define 'sample_name' fixture in sample test driver")
45-
46-
4736
@pytest.fixture(scope="module")
4837
def database_dialect():
4938
"""Database dialect to be used for this sample.
@@ -99,31 +88,31 @@ def cleanup_old_instances(spanner_client):
9988
scrub_instance_ignore_not_found(inst)
10089

10190

102-
@pytest.fixture(scope="module")
91+
@pytest.fixture(scope="session")
10392
def instance_id():
10493
"""Unique id for the instance used in samples."""
10594
return f"test-instance-{uuid.uuid4().hex[:10]}"
10695

10796

108-
@pytest.fixture(scope="module")
97+
@pytest.fixture(scope="session")
10998
def multi_region_instance_id():
11099
"""Unique id for the multi-region instance used in samples."""
111100
return f"multi-instance-{uuid.uuid4().hex[:10]}"
112101

113102

114-
@pytest.fixture(scope="module")
103+
@pytest.fixture(scope="session")
115104
def instance_config(spanner_client):
116105
return "{}/instanceConfigs/{}".format(
117106
spanner_client.project_name, "regional-us-central1"
118107
)
119108

120109

121-
@pytest.fixture(scope="module")
110+
@pytest.fixture(scope="session")
122111
def multi_region_instance_config(spanner_client):
123112
return "{}/instanceConfigs/{}".format(spanner_client.project_name, "nam3")
124113

125114

126-
@pytest.fixture(scope="module")
115+
@pytest.fixture(scope="session")
127116
def proto_descriptor_file():
128117
import os
129118

@@ -134,13 +123,12 @@ def proto_descriptor_file():
134123
file.close()
135124

136125

137-
@pytest.fixture(scope="module")
126+
@pytest.fixture(scope="session")
138127
def sample_instance(
139128
spanner_client,
140129
cleanup_old_instances,
141130
instance_id,
142131
instance_config,
143-
sample_name,
144132
):
145133
operation = spanner_client.instance_admin_api.create_instance(
146134
parent=spanner_client.project_name,
@@ -151,7 +139,7 @@ def sample_instance(
151139
node_count=1,
152140
labels={
153141
"cloud_spanner_samples": "true",
154-
"sample_name": sample_name,
142+
"sample_name": "shared-samples",
155143
"created": str(int(time.time())),
156144
},
157145
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE_PLUS, # Optional
@@ -170,20 +158,19 @@ def sample_instance(
170158
scrub_instance_ignore_not_found(sample_instance)
171159

172160

173-
@pytest.fixture(scope="module")
161+
@pytest.fixture(scope="session")
174162
def multi_region_instance(
175163
spanner_client,
176164
cleanup_old_instances,
177165
multi_region_instance_id,
178166
multi_region_instance_config,
179-
sample_name,
180167
):
181168
multi_region_instance = spanner_client.instance(
182169
multi_region_instance_id,
183170
multi_region_instance_config,
184171
labels={
185172
"cloud_spanner_samples": "true",
186-
"sample_name": sample_name,
173+
"sample_name": "shared-samples",
187174
"created": str(int(time.time())),
188175
},
189176
)
@@ -205,7 +192,7 @@ def database_id():
205192
206193
Sample testcase modules can override as needed.
207194
"""
208-
return "my-database-id"
195+
return f"my-db-{uuid.uuid4().hex[:10]}"
209196

210197

211198
@pytest.fixture(scope="module")

packages/google-cloud-spanner/samples/samples/graph_snippets_test.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@
8080
LABEL Transfers)
8181
"""
8282

83-
84-
@pytest.fixture(scope="module")
85-
def sample_name():
86-
return "snippets"
87-
88-
8983
@pytest.fixture(scope="module")
9084
def database_dialect():
9185
"""Spanner dialect to be used for this sample.

packages/google-cloud-spanner/samples/samples/pg_snippets_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@
4646
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)
4747

4848

49-
@pytest.fixture(scope="module")
50-
def sample_name():
51-
return "pg_snippets"
52-
53-
5449
@pytest.fixture(scope="module")
5550
def database_dialect():
5651
"""Spanner dialect to be used for this sample.

packages/google-cloud-spanner/samples/samples/quickstart_test.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
import quickstart
1818

1919

20-
@pytest.fixture(scope="module")
21-
def sample_name():
22-
return "quickstart"
23-
24-
2520
def test_quickstart(capsys, instance_id, sample_database):
2621
quickstart.run_quickstart(instance_id, sample_database.database_id)
2722
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)