Skip to content

Commit b49322b

Browse files
ohmayrchalmerlowe
authored andcommitted
fix: make test_utils unique_resource_id parallel-safe (#17440)
This PR shifts the parallel-safety logic (`os.getpid()`) out of the individual firestore system tests, baking it directly into the shared `test_utils` package. This ensures that all packages in the monorepo using pytest-xdist will automatically generate unique collection/resource names that never collide across parallel workers, rather than requiring each library to manually append PID strings to `unique_resource_id()`.
1 parent 8aec8d6 commit b49322b

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

.librarian/generator-input/client-post-processing/firestore-integration.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,31 @@ replacements:
564564
"freezegun",
565565
]
566566
count: 1
567+
- paths: [
568+
packages/google-cloud-firestore/noxfile.py
569+
]
570+
before: |
571+
SYSTEM_TEST_STANDARD_DEPENDENCIES = \[
572+
"mock",
573+
"pytest",
574+
"google-cloud-testutils",
575+
\]
576+
after: |
577+
SYSTEM_TEST_STANDARD_DEPENDENCIES = [
578+
"mock",
579+
"pytest",
580+
]
581+
count: 1
582+
- paths: [
583+
packages/google-cloud-firestore/noxfile.py
584+
]
585+
before: |
586+
SYSTEM_TEST_LOCAL_DEPENDENCIES: List\[str\] = \[\]
587+
after: |
588+
SYSTEM_TEST_LOCAL_DEPENDENCIES: List[str] = [
589+
"../google-cloud-testutils"
590+
]
591+
count: 1
567592
# TODO(https://github.com/googleapis/google-cloud-python/issues/17429):
568593
# Temporary post-processing rule to add pytest-xdist dependency.
569594
# Remove this once gapic-generator includes pytest-xdist by default.

packages/google-cloud-firestore/noxfile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,14 @@
7676
SYSTEM_TEST_STANDARD_DEPENDENCIES = [
7777
"mock",
7878
"pytest",
79-
"google-cloud-testutils",
8079
]
8180
SYSTEM_TEST_EXTERNAL_DEPENDENCIES: List[str] = [
8281
"pytest-asyncio==0.21.2",
8382
"six",
8483
"pyyaml",
8584
"pytest-xdist",
8685
]
87-
SYSTEM_TEST_LOCAL_DEPENDENCIES: List[str] = []
86+
SYSTEM_TEST_LOCAL_DEPENDENCIES: List[str] = ["../google-cloud-testutils"]
8887
SYSTEM_TEST_DEPENDENCIES: List[str] = []
8988
SYSTEM_TEST_EXTRAS: List[str] = []
9089
SYSTEM_TEST_EXTRAS_BY_PYTHON: Dict[str, List[str]] = {}

packages/google-cloud-firestore/tests/system/test__helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
MISSING_DOCUMENT = "No document to update: "
1717
DOCUMENT_EXISTS = "Document already exists: "
1818
ENTERPRISE_MODE_ERROR = "only allowed on ENTERPRISE mode"
19-
UNIQUE_RESOURCE_ID = unique_resource_id("-") + "-" + str(os.getpid())
19+
UNIQUE_RESOURCE_ID = unique_resource_id("-")
2020
EMULATOR_CREDS = EmulatorCreds()
2121
FIRESTORE_EMULATOR = os.environ.get(_FIRESTORE_EMULATOR_HOST) is not None
2222
FIRESTORE_OTHER_DB = os.environ.get("SYSTEM_TESTS_DATABASE", "system-tests-named-db")

packages/google-cloud-testutils/test_utils/system.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ def unique_resource_id(delimiter="_"):
7474
testing environments and at particular times.
7575
"""
7676
build_id = os.getenv("CIRCLE_BUILD_NUM", "")
77+
pid = os.getpid()
7778
if build_id == "":
78-
return "%s%d" % (delimiter, 1000 * time.time())
79+
return "%s%d%s%d" % (delimiter, 1000 * time.time(), delimiter, pid)
7980
else:
80-
return "%s%s%s%d" % (delimiter, build_id, delimiter, time.time())
81+
return "%s%s%s%d%s%d" % (
82+
delimiter,
83+
build_id,
84+
delimiter,
85+
time.time(),
86+
delimiter,
87+
pid,
88+
)

0 commit comments

Comments
 (0)