ExLLM.Testing.TestCacheHelpers (ex_llm v0.8.1)

View Source

Test helper functions for managing and working with the automatic test cache.

This module provides convenient functions for test suites to interact with the caching system, including cache warming, clearing, and debugging.

Summary

Functions

Clear test cache with different scopes.

Deduplicate cache content to save space.

Disable debug logging for cache operations.

Enable debug logging for cache operations.

Force cache miss for specific patterns.

Force cache refresh for specific patterns.

Get cache statistics for specific test module.

List all cache timestamps for a pattern.

Print cache statistics summary.

Print cache statistics by provider.

Restore cache from a specific timestamp.

Set fallback strategy for specific test patterns.

Set custom TTL for specific test patterns.

Setup hook for test modules to initialize caching.

Verify cache integrity and check for issues.

Warm test cache for specific test modules or patterns.

Run a test with specific cache configuration.

Functions

cleanup_old_timestamps(max_age \\ 30 * 24 * 60 * 60 * 1000)

Clean up old cache timestamps.

clear_test_cache(scope \\ :all)

Clear test cache with different scopes.

Examples

# Clear all cache
clear_test_cache(:all)

# Clear specific provider
clear_test_cache("anthropic")

# Clear specific test module
clear_test_cache("AnthropicIntegrationTest")

deduplicate_cache_content(cache_pattern \\ :all)

Deduplicate cache content to save space.

disable_cache_debug()

Disable debug logging for cache operations.

enable_cache_debug()

Enable debug logging for cache operations.

force_cache_miss(pattern)

Force cache miss for specific patterns.

Useful for testing real API calls without disabling cache entirely.

force_cache_refresh(pattern)

Force cache refresh for specific patterns.

Ignores TTL and forces fresh API calls.

get_cache_stats(test_module \\ :all)

Get cache statistics for specific test module.

list_cache_timestamps(cache_pattern)

List all cache timestamps for a pattern.

Examples

list_cache_timestamps("anthropic/chat_basic")

restore_cache_timestamp(cache_pattern, timestamp)

Restore cache from a specific timestamp.

Useful for debugging or testing with older responses.

set_fallback_strategy(test_pattern, strategy)

Set fallback strategy for specific test patterns.

set_test_ttl(test_pattern, ttl)

Set custom TTL for specific test patterns.

setup_test_cache(context \\ %{})

Setup hook for test modules to initialize caching.

Usage in test module:

setup do
  ExLLM.Testing.TestCacheHelpers.setup_test_cache()
end

verify_cache_integrity()

Verify cache integrity and check for issues.

warm_test_cache(test_module)

Warm test cache for specific test modules or patterns.

This pre-loads cache entries that might be expiring soon.

with_test_cache(opts \\ [], func)

Run a test with specific cache configuration.

Examples

with_test_cache([ttl: :timer.hours(1)], fn ->
  # Your test code here
end)