Commit 522ba9b
authored
test: Fix Redis-backed test isolation across xdist runs (#543)
## Summary
This PR reduces flaky Redis-backed service tests by fixing test
isolation issues in the test suite rather than changing product code.
The failures were caused by Redis state leaking across tests during
`pytest -n auto` runs. Several tests reused fixed Redis resource names,
and some shared HNSW index fixtures created Redis indexes without
deleting them. That made unrelated tests on the same xdist worker
sensitive to execution order and leftover state, which explains why some
matrix failures passed on retry.
## Problem
The main issue was cross-test persistence in Redis:
- Shared fixtures used Redis index names and prefixes scoped only to
`worker_id`.
- `hnsw_index` and `async_hnsw_index` created indexes but did not tear
them down.
- Several integration tests created histories, routers, caches, or
indexes with fixed names like `test_app`, `float64 history`,
`test_pass_through_dtype`, and `test_multi_prefix`.
In CI, that meant one test could leave Redis resources behind for
another test running later on the same worker. The result was
intermittent failures that were noisy, hard to trust, and often
disappeared on rerun.
## Solution
This PR makes Redis-backed test resources unique per test and restores
missing cleanup.
A shared pytest helper now generates per-test Redis names using the
worker id plus the current test node. That helper is used in the shared
search-index fixtures and in the highest-risk integration tests that
previously reused fixed names. The HNSW fixtures now delete their
indexes after yielding, and tests that instantiate Redis-backed objects
directly now clean them up explicitly.
The semantic router tests also no longer need the existing “flaky test”
skips because their Redis setup is now deterministic under unique names.
## Changes
- Added a per-test Redis naming fixture in `tests/conftest.py`
- Updated shared flat/HNSW index fixtures to use per-test index names
and prefixes
- Added teardown to `hnsw_index` and `async_hnsw_index`
- Replaced fixed Redis names in `test_message_history.py`
- Replaced fixed Redis names in `test_semantic_router.py`
- Added explicit cleanup for direct `SemanticMessageHistory` and
`SemanticRouter` instances
- Updated embed cache fixtures to use per-test cache names
- Updated the multiple-prefix existing-index test to use a unique index
name
## Scope
- Test-only changes
- No production API changes
- No CI workflow changes
- No coverage reduction
## Verification
- Targeted Redis-backed suites passed under `pytest -n auto`
- Repeated reruns showed the isolation fixes holding at the assertion
level
- There is still a separate pytest master shutdown hang during some
repeated xdist reruns, but that is distinct from the Redis leakage fixed
here
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Low risk: changes are limited to test fixtures/assertions, mainly
improving Redis resource isolation and teardown to reduce CI flakiness
under `pytest-xdist`. Main risk is unintended name/cleanup changes
masking real failures or leaving less coverage for shared-state
scenarios.
>
> **Overview**
> Improves Redis-backed test isolation under `pytest -n` by introducing
a `redis_test_name` fixture (worker id + test node hash) and switching
indices/caches/histories/routers to use per-test Redis names instead of
fixed or worker-scoped ones.
>
> Adds missing teardown for HNSW index fixtures and wraps several
integration tests in explicit `clear()`/`delete()` cleanup blocks; also
removes prior *flaky test* skips and updates a hybrid-search assertion
to match the actual number of loaded docs.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
7e2afe8. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent a98dcde commit 522ba9b
6 files changed
Lines changed: 331 additions & 176 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
36 | 50 | | |
37 | 51 | | |
38 | 52 | | |
| |||
466 | 480 | | |
467 | 481 | | |
468 | 482 | | |
469 | | - | |
| 483 | + | |
470 | 484 | | |
471 | 485 | | |
472 | 486 | | |
473 | 487 | | |
474 | 488 | | |
| 489 | + | |
| 490 | + | |
475 | 491 | | |
476 | 492 | | |
477 | 493 | | |
478 | | - | |
479 | | - | |
| 494 | + | |
| 495 | + | |
480 | 496 | | |
481 | 497 | | |
482 | 498 | | |
| |||
521 | 537 | | |
522 | 538 | | |
523 | 539 | | |
524 | | - | |
| 540 | + | |
525 | 541 | | |
526 | 542 | | |
527 | 543 | | |
528 | 544 | | |
529 | 545 | | |
| 546 | + | |
| 547 | + | |
530 | 548 | | |
531 | 549 | | |
532 | 550 | | |
533 | | - | |
534 | | - | |
| 551 | + | |
| 552 | + | |
535 | 553 | | |
536 | 554 | | |
537 | 555 | | |
| |||
576 | 594 | | |
577 | 595 | | |
578 | 596 | | |
579 | | - | |
| 597 | + | |
580 | 598 | | |
581 | 599 | | |
582 | 600 | | |
583 | 601 | | |
| 602 | + | |
| 603 | + | |
584 | 604 | | |
585 | 605 | | |
586 | 606 | | |
587 | | - | |
588 | | - | |
| 607 | + | |
| 608 | + | |
589 | 609 | | |
590 | 610 | | |
591 | 611 | | |
| |||
625 | 645 | | |
626 | 646 | | |
627 | 647 | | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
628 | 651 | | |
629 | 652 | | |
630 | | - | |
| 653 | + | |
631 | 654 | | |
632 | 655 | | |
633 | 656 | | |
634 | 657 | | |
| 658 | + | |
| 659 | + | |
635 | 660 | | |
636 | 661 | | |
637 | 662 | | |
638 | | - | |
639 | | - | |
| 663 | + | |
| 664 | + | |
640 | 665 | | |
641 | 666 | | |
642 | 667 | | |
| |||
676 | 701 | | |
677 | 702 | | |
678 | 703 | | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
679 | 707 | | |
680 | 708 | | |
681 | 709 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
| 139 | + | |
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| |||
0 commit comments