|
17 | 17 | import sentry_sdk |
18 | 18 | from django.conf import settings |
19 | 19 |
|
| 20 | +from sentry.testutils.pytest import xdist |
20 | 21 | from sentry.runner.importer import install_plugin_apps |
21 | 22 | from sentry.silo.base import SiloMode |
22 | 23 | from sentry.testutils.region import TestEnvRegionDirectory |
|
32 | 33 | os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir, "tests") |
33 | 34 | ) |
34 | 35 |
|
35 | | -TEST_REDIS_DB = 9 |
36 | | - |
37 | | - |
38 | 36 | def _use_monolith_dbs() -> bool: |
39 | 37 | return os.environ.get("SENTRY_USE_MONOLITH_DBS", "0") == "1" |
40 | 38 |
|
@@ -69,10 +67,21 @@ def _configure_test_env_regions() -> None: |
69 | 67 | # Assign a random name on every test run, as a reminder that test setup and |
70 | 68 | # assertions should not depend on this value. If you need to test behavior that |
71 | 69 | # depends on region attributes, use `override_regions` in your test case. |
72 | | - region_name = "testregion" + "".join(random.choices(string.digits, k=6)) |
| 70 | + # Under xdist, seed deterministically so all workers generate the same name |
| 71 | + # (divergent names break xdist's requirement for identical test collection). |
| 72 | + xdist_uid = os.environ.get("PYTEST_XDIST_TESTRUNUID") |
| 73 | + r = random.Random(xdist_uid) if xdist_uid else random |
| 74 | + region_name = "testregion" + "".join(r.choices(string.digits, k=6)) |
| 75 | + |
| 76 | + # Under xdist, each worker gets a unique snowflake_id (1, 2, 3, ...) so |
| 77 | + # concurrent model creation doesn't produce colliding IDs. |
| 78 | + region_snowflake_id = xdist._worker_num + 1 if xdist._worker_num is not None else 0 |
73 | 79 |
|
74 | 80 | default_region = Region( |
75 | | - region_name, 0, settings.SENTRY_OPTIONS["system.url-prefix"], RegionCategory.MULTI_TENANT |
| 81 | + region_name, |
| 82 | + region_snowflake_id, |
| 83 | + settings.SENTRY_OPTIONS["system.url-prefix"], |
| 84 | + RegionCategory.MULTI_TENANT, |
76 | 85 | ) |
77 | 86 |
|
78 | 87 | settings.SENTRY_REGION = region_name |
@@ -196,14 +205,17 @@ def pytest_configure(config: pytest.Config) -> None: |
196 | 205 | settings.SENTRY_RATELIMITER = "sentry.ratelimits.redis.RedisRateLimiter" |
197 | 206 | settings.SENTRY_RATELIMITER_OPTIONS = {} |
198 | 207 |
|
| 208 | + if snuba_url := xdist.get_snuba_url(): |
| 209 | + settings.SENTRY_SNUBA = snuba_url |
| 210 | + |
199 | 211 | settings.SENTRY_ISSUE_PLATFORM_FUTURES_MAX_LIMIT = 1 |
200 | 212 |
|
201 | 213 | if not hasattr(settings, "SENTRY_OPTIONS"): |
202 | 214 | settings.SENTRY_OPTIONS = {} |
203 | 215 |
|
204 | 216 | settings.SENTRY_OPTIONS.update( |
205 | 217 | { |
206 | | - "redis.clusters": {"default": {"hosts": {0: {"db": TEST_REDIS_DB}}}}, |
| 218 | + "redis.clusters": {"default": {"hosts": {0: {"db": xdist.get_redis_db()}}}}, |
207 | 219 | "mail.backend": "django.core.mail.backends.locmem.EmailBackend", |
208 | 220 | "system.url-prefix": "http://testserver", |
209 | 221 | "system.base-hostname": "testserver", |
|
0 commit comments