Optimize Redis test fixtures
- Dominant language
- Python
- Stars
- 670
- Forks
- 183
- Avg merge
- 17h 7m
- Merged PRs (30d)
- 358
Description
## Overview
Optimize Redis test fixtures by changing scope and separating Sentinel cluster tests.
## Current Problem
|Test File|Time|Tests|Issue|
|---|---|---|---|
|`test_pipeline.py`|30.91s|3|Redis container per function|
|`test_connect.py`|27.91s|4|Redis container per function|
- `redis_container` fixture has function scope
- Container startup (~8-10s) happens for each test
- Sentinel cluster tests mixed with simple Redis tests
## Target Files
- `tests/unit/common/redis_helper/conftest.py`
- `src/ai/backend/testutils/bootstrap.py`
## Work Items
### 1. Change `redis_container` to Session Scope
```python
# tests/unit/common/redis_helper/conftest.py
@pytest.fixture(scope="session")
def redis_container():
with RedisContainer("redis:7") as redis:
yield redis
@pytest.fixture(scope="session")
async def redis_connection(redis_container):
url = redis_container.get_connection_url()
client = Redis.from_url(url)
yield client
await client.close()
```
### 2. Separate Sentinel Cluster Tests
```python
# Add marker for Sentinel tests
@pytest.mark.redis_sentinel
async def test_sentinel_failover():
...
# conftest.py
def pytest_configure(config):
config.addinivalue_line(
"markers", "redis_sentinel: marks tests requiring Redis Sentinel cluster"
)
```
### 3. Data Isolation Between Tests
```python
@pytest.fixture
async def clean_redis(redis_connection):
yield redis_connection
await redis_connection.flushdb() # Clean after each test
```
## Expected Improvement
|Metric|Before|After|
|---|---|---|
|Container startups|7 times|1 time|
|Total time|~59s|~22s|
|Savings|\*|~37s|
## Acceptance Criteria
- [ ] `redis_container` changed to session scope
- [ ] Data isolation maintained between tests
- [ ] Sentinel tests marked with `@pytest.mark.redis_sentinel`
- [ ] All Redis tests pass
- [ ] Test execution time reduced by ~60%
## Parent Epic
BA-3611
JIRA Issue: BA-3616
Contributor guide
Assessment
This issue has not been assessed yet.