Skip to content

Commit

Permalink
fix: remove delete of instance if it already exists, as it might be i…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
vi3k6i5 committed May 31, 2021
1 parent b1f49f7 commit 0544208
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions run_testing_worker.py
Expand Up @@ -14,6 +14,8 @@
from google.cloud.spanner_v1 import Client

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


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

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

name = "spanner-django-test-{}".format(str(int(time.time())))
name = "sp-dj-test-{}-{}".format(
str(WORKER_INDEX), str(int(time.time()))
)

self._instance = client.instance(name, config)
if self._instance.exists():
# If test instance already exists first delete it and then create.
self._instance.delete()
created_op.result(120) # block until completion
created_op = self._instance.create()
created_op.result(120) # block until completion
return name
Expand All @@ -42,24 +42,21 @@ def __exit__(self, exc, exc_value, traceback):
self._instance.delete()


worker_index = int(os.getenv("DJANGO_WORKER_INDEX", 0))
worker_count = int(os.getenv("DJANGO_WORKER_COUNT", 1))

if worker_index >= worker_count:
if WORKER_INDEX >= WORKER_COUNT:
print(
"worker_index ({wi}) > worker_count ({wc})".format(
wi=worker_index,
wc=worker_count,
"WORKER_INDEX ({wi}) > WORKER_COUNT ({wc})".format(
wi=WORKER_INDEX,
wc=WORKER_COUNT,
)
)
exit()

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

apps_per_worker = math.ceil(len(all_apps) / worker_count)
apps_per_worker = math.ceil(len(all_apps) / WORKER_COUNT)

start_index = min(worker_index * apps_per_worker, len(all_apps))
start_index = min(WORKER_INDEX * apps_per_worker, len(all_apps))
end_index = min(start_index + apps_per_worker, len(all_apps))

test_apps = all_apps[start_index:end_index]
Expand Down

0 comments on commit 0544208

Please sign in to comment.