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

Commit 0544208

Browse files
authored
fix: remove delete of instance if it already exists, as it might be in use by another test (#641)
* test: fix run testing worker code to reduce the chance of same instance id being used by different workers * fix: remove delete of instance if it already exists, as it might be in use by another test
1 parent b1f49f7 commit 0544208

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

run_testing_worker.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from google.cloud.spanner_v1 import Client
1515

1616
REGION = "regional-us-central1"
17+
WORKER_INDEX = int(os.getenv("DJANGO_WORKER_INDEX", 0))
18+
WORKER_COUNT = int(os.getenv("DJANGO_WORKER_COUNT", 1))
1719

1820

1921
class TestInstance:
@@ -27,13 +29,11 @@ def __enter__(self):
2729

2830
config = f"{client.project_name}/instanceConfigs/{REGION}"
2931

30-
name = "spanner-django-test-{}".format(str(int(time.time())))
32+
name = "sp-dj-test-{}-{}".format(
33+
str(WORKER_INDEX), str(int(time.time()))
34+
)
3135

3236
self._instance = client.instance(name, config)
33-
if self._instance.exists():
34-
# If test instance already exists first delete it and then create.
35-
self._instance.delete()
36-
created_op.result(120) # block until completion
3737
created_op = self._instance.create()
3838
created_op.result(120) # block until completion
3939
return name
@@ -42,24 +42,21 @@ def __exit__(self, exc, exc_value, traceback):
4242
self._instance.delete()
4343

4444

45-
worker_index = int(os.getenv("DJANGO_WORKER_INDEX", 0))
46-
worker_count = int(os.getenv("DJANGO_WORKER_COUNT", 1))
47-
48-
if worker_index >= worker_count:
45+
if WORKER_INDEX >= WORKER_COUNT:
4946
print(
50-
"worker_index ({wi}) > worker_count ({wc})".format(
51-
wi=worker_index,
52-
wc=worker_count,
47+
"WORKER_INDEX ({wi}) > WORKER_COUNT ({wc})".format(
48+
wi=WORKER_INDEX,
49+
wc=WORKER_COUNT,
5350
)
5451
)
5552
exit()
5653

5754
with open("django_test_apps.txt", "r") as file:
5855
all_apps = file.read().split("\n")
5956

60-
apps_per_worker = math.ceil(len(all_apps) / worker_count)
57+
apps_per_worker = math.ceil(len(all_apps) / WORKER_COUNT)
6158

62-
start_index = min(worker_index * apps_per_worker, len(all_apps))
59+
start_index = min(WORKER_INDEX * apps_per_worker, len(all_apps))
6360
end_index = min(start_index + apps_per_worker, len(all_apps))
6461

6562
test_apps = all_apps[start_index:end_index]

0 commit comments

Comments
 (0)