Skip to content

Testing: Missing concurrency stress tests for edge cases #6

@tanbro

Description

@tanbro

Severity

High

Location

File: tests/test_threads.py and general test coverage

Description

While there are basic concurrency tests (test_two_threads, test_pool_map), there are no stress tests for critical edge cases:

  1. Cache stampede scenarios: Multiple concurrent requests with same arguments hitting cache miss
  2. High contention on single keys: Many threads accessing same cache key simultaneously
  3. Redis connection failures: Cache behavior during network issues
  4. Concurrent eviction during writes: Race conditions during maxsize eviction

Impact

The documented race condition (README.md:1029-1030) is not tested or demonstrated, making it difficult to verify fixes.

Proposed Tests

async def test_cache_stampede_concurrent_misses():
    \"\"\"Test that concurrent misses don't all execute the function.\"\"\"
    cache = RedisFuncCache("stampede", LruTPolicy(), ...)
    
    call_count = 0
    @cache
    async def expensive_func(x):
        nonlocal call_count
        call_count += 1
        await asyncio.sleep(0.5)  # Simulate slow operation
        return x
    
    # 10 concurrent calls with same arguments
    results = await asyncio.gather(*[expensive_func(42) for _ in range(10)])
    
    # With proper locking, call_count should be ~1-2, not 10
    assert call_count < 5  # Allow some tolerance

async def test_high_contention_single_key():
    \"\"\"Test many concurrent accesses to the same key.\"\"\"
    # 100+ concurrent operations on single cache entry
    
async def test_redis_connection_failure():
    \"\"\"Test cache behavior when Redis becomes unavailable.\"\"\"
    # Should fail gracefully or use fallback
    
async def test_concurrent_eviction():
    \"\"\"Test concurrent writes during maxsize eviction.\"\"\"
    # Multiple threads triggering eviction simultaneously

Additional Context

  • Use asyncio.gather() or ThreadPoolExecutor for concurrency
  • Use slower functions to expose stampede vulnerability
  • Consider using pytest-asyncio for async test support
  • Tests should validate both correctness and performance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions