You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test(spanner): add pytest-xdist parallel execution with state isolation (#17344)
This PR adds `pytest-xdist` to parallelize unit tests and the
`core_deps_from_source` nox sessions for the `google-cloud-spanner`
package.
**By running tests in parallel using `-n auto`, the execution time of
the Spanner unit tests are reduced from ~8 minutes to 4 minutes.**
State isolation and test reliability are achieved by:
* **Simplifying Subtests**: We pass simple strings/names to `subTest()`
instead of complex objects. This keeps subtests lightweight and prevents
serialization errors.
* **Cleaning Up Global Singletons**: We reset telemetry singleton states
on test teardown (using pytest's idiomatic `monkeypatch` fixture). This
ensures metric counters don't leak from one test into another.
* **Fixing Concurrent Mock Conflicts**: We return fresh mocked iterators
for concurrent calls (using `side_effect` instead of `return_value`).
This prevents one thread from exhausting a mock's results before another
thread can read them.
* **Robust Assertion Checks**: We added a helper method
(`_assert_concurrent_transaction_invariants`) that verifies the
*behavior* of concurrent threads (ensuring exactly one thread starts the
transaction while others wait/reuse it), rather than checking fragile
call logs or counting sequential request IDs. This allowed us to safely
run four concurrent tests.
> [!note]
> The long pole in the tent is still the `system` tests, which require
about 30 minutes. It is not as simple as just adding xdist because there
are other factors that limit velocity including the fact that system
tests actually interact with live systems.
0 commit comments