|
10 | 10 | from redisvl.utils.vectorize import HFTextVectorizer |
11 | 11 |
|
12 | 12 |
|
| 13 | +@pytest.fixture(scope="session") |
| 14 | +def worker_id(request): |
| 15 | + """ |
| 16 | + Get the worker ID for the current test. |
| 17 | +
|
| 18 | + In pytest-xdist, the config has "workerid" in workerinput. |
| 19 | + This fixture abstracts that logic to provide a consistent worker_id |
| 20 | + across all tests. |
| 21 | + """ |
| 22 | + workerinput = getattr(request.config, "workerinput", {}) |
| 23 | + return workerinput.get("workerid", "master") |
| 24 | + |
| 25 | + |
13 | 26 | @pytest.fixture(autouse=True) |
14 | 27 | def set_tokenizers_parallelism(): |
15 | 28 | """Disable tokenizers parallelism in tests to avoid deadlocks""" |
16 | 29 | os.environ["TOKENIZERS_PARALLELISM"] = "false" |
17 | 30 |
|
18 | 31 |
|
19 | 32 | @pytest.fixture(scope="session", autouse=True) |
20 | | -def redis_container(request): |
| 33 | +def redis_container(worker_id): |
21 | 34 | """ |
22 | 35 | If using xdist, create a unique Compose project for each xdist worker by |
23 | 36 | setting COMPOSE_PROJECT_NAME. That prevents collisions on container/volume |
24 | 37 | names. |
25 | 38 | """ |
26 | | - # In xdist, the config has "workerid" in workerinput |
27 | | - workerinput = getattr(request.config, "workerinput", {}) |
28 | | - worker_id = workerinput.get("workerid", "master") |
29 | | - |
30 | 39 | # Set the Compose project name so containers do not clash across workers |
31 | 40 | os.environ["COMPOSE_PROJECT_NAME"] = f"redis_test_{worker_id}" |
32 | 41 | os.environ.setdefault("REDIS_IMAGE", "redis/redis-stack-server:latest") |
@@ -206,16 +215,17 @@ def pytest_collection_modifyitems( |
206 | 215 |
|
207 | 216 |
|
208 | 217 | @pytest.fixture |
209 | | -def flat_index(sample_data, redis_url): |
| 218 | +def flat_index(sample_data, redis_url, worker_id): |
210 | 219 | """ |
211 | 220 | A fixture that uses the "flag" algorithm for its vector field. |
212 | 221 | """ |
| 222 | + |
213 | 223 | # construct a search index from the schema |
214 | 224 | index = SearchIndex.from_dict( |
215 | 225 | { |
216 | 226 | "index": { |
217 | | - "name": "user_index", |
218 | | - "prefix": "v1", |
| 227 | + "name": f"user_index_{worker_id}", |
| 228 | + "prefix": f"v1_{worker_id}", |
219 | 229 | "storage_type": "hash", |
220 | 230 | }, |
221 | 231 | "fields": [ |
@@ -260,16 +270,17 @@ def hash_preprocess(item: dict) -> dict: |
260 | 270 |
|
261 | 271 |
|
262 | 272 | @pytest.fixture |
263 | | -async def async_flat_index(sample_data, redis_url): |
| 273 | +async def async_flat_index(sample_data, redis_url, worker_id): |
264 | 274 | """ |
265 | 275 | A fixture that uses the "flag" algorithm for its vector field. |
266 | 276 | """ |
| 277 | + |
267 | 278 | # construct a search index from the schema |
268 | 279 | index = AsyncSearchIndex.from_dict( |
269 | 280 | { |
270 | 281 | "index": { |
271 | | - "name": "user_index", |
272 | | - "prefix": "v1", |
| 282 | + "name": f"user_index_{worker_id}", |
| 283 | + "prefix": f"v1_{worker_id}", |
273 | 284 | "storage_type": "hash", |
274 | 285 | }, |
275 | 286 | "fields": [ |
@@ -314,15 +325,16 @@ def hash_preprocess(item: dict) -> dict: |
314 | 325 |
|
315 | 326 |
|
316 | 327 | @pytest.fixture |
317 | | -async def async_hnsw_index(sample_data, redis_url): |
| 328 | +async def async_hnsw_index(sample_data, redis_url, worker_id): |
318 | 329 | """ |
319 | 330 | A fixture that uses the "hnsw" algorithm for its vector field. |
320 | 331 | """ |
| 332 | + |
321 | 333 | index = AsyncSearchIndex.from_dict( |
322 | 334 | { |
323 | 335 | "index": { |
324 | | - "name": "user_index", |
325 | | - "prefix": "v1", |
| 336 | + "name": f"user_index_{worker_id}", |
| 337 | + "prefix": f"v1_{worker_id}", |
326 | 338 | "storage_type": "hash", |
327 | 339 | }, |
328 | 340 | "fields": [ |
@@ -364,15 +376,16 @@ def hash_preprocess(item: dict) -> dict: |
364 | 376 |
|
365 | 377 |
|
366 | 378 | @pytest.fixture |
367 | | -def hnsw_index(sample_data, redis_url): |
| 379 | +def hnsw_index(sample_data, redis_url, worker_id): |
368 | 380 | """ |
369 | 381 | A fixture that uses the "hnsw" algorithm for its vector field. |
370 | 382 | """ |
| 383 | + |
371 | 384 | index = SearchIndex.from_dict( |
372 | 385 | { |
373 | 386 | "index": { |
374 | | - "name": "user_index", |
375 | | - "prefix": "v1", |
| 387 | + "name": f"user_index_{worker_id}", |
| 388 | + "prefix": f"v1_{worker_id}", |
376 | 389 | "storage_type": "hash", |
377 | 390 | }, |
378 | 391 | "fields": [ |
|
0 commit comments