Package com.redis.vl.test.vcr
Class VCRContext
java.lang.Object
com.redis.vl.test.vcr.VCRContext
Manages VCR state and resources throughout a test session.
VCRContext handles:
- Redis container lifecycle with persistence
- Test context tracking
- Call counter management for cassette key generation
- Statistics collection
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringEnvironment variable name for overriding VCR mode. -
Constructor Summary
ConstructorsConstructorDescriptionVCRContext(VCRTest config) Creates a new VCR context with the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoiddeleteCassettes(List<String> keys) Deletes the specified cassettes.generateCassetteKey(String type) Generates a unique cassette key for the current test and call type.longGets the API call count.longGets the cache hit count.longGets the cache miss count.Gets the cassette store for storing/retrieving cassettes.Gets the configured VCR mode.Gets the cassette keys generated for the current test.Gets the current test ID.Gets the data directory path.Gets the effective VCR mode for the current test.redis.clients.jedis.UnifiedJedisgetJedis()Gets the Redis client.Gets the VCR registry.voidInitializes the VCR context, starting Redis and loading existing cassettes.voidPersists cassettes by triggering a Redis BGSAVE.voidPrints VCR statistics to stdout.voidRecords an API call.voidRecords a cache hit.voidRecords a cache miss.voidResets call counters for a new test.voidsetCurrentTest(String testId) Sets the current test context.voidsetEffectiveMode(VCRMode mode) Sets the effective VCR mode.voidshutdown()Shuts down the VCR context and releases resources.
-
Field Details
-
VCR_MODE_ENV
Environment variable name for overriding VCR mode.- See Also:
-
-
Constructor Details
-
VCRContext
Creates a new VCR context with the given configuration.The VCR mode can be overridden via the
VCR_MODEenvironment variable. Valid values are: PLAYBACK, PLAYBACK_OR_RECORD, RECORD, OFF. If the environment variable is set, it takes precedence over the annotation's mode setting.- Parameters:
config- the VCR test configuration
-
-
Method Details
-
initialize
Initializes the VCR context, starting Redis and loading existing cassettes.- Throws:
Exception- if initialization fails
-
getCassetteStore
Gets the cassette store for storing/retrieving cassettes.- Returns:
- the cassette store
-
shutdown
public void shutdown()Shuts down the VCR context and releases resources. -
resetCallCounters
public void resetCallCounters()Resets call counters for a new test. -
setCurrentTest
Sets the current test context.- Parameters:
testId- the unique test identifier
-
getCurrentTestId
Gets the current test ID.- Returns:
- the current test ID
-
generateCassetteKey
Generates a unique cassette key for the current test and call type.- Parameters:
type- the type of call (e.g., "llm", "embedding")- Returns:
- the generated cassette key
-
getCurrentCassetteKeys
Gets the cassette keys generated for the current test.- Returns:
- list of cassette keys
-
deleteCassettes
Deletes the specified cassettes.- Parameters:
keys- the cassette keys to delete
-
persistCassettes
public void persistCassettes()Persists cassettes by triggering a Redis BGSAVE. -
getRegistry
Gets the VCR registry.- Returns:
- the registry
-
getConfiguredMode
Gets the configured VCR mode.- Returns:
- the configured mode
-
getEffectiveMode
Gets the effective VCR mode for the current test.- Returns:
- the effective mode
-
setEffectiveMode
Sets the effective VCR mode.- Parameters:
mode- the mode to set
-
getJedis
public redis.clients.jedis.UnifiedJedis getJedis()Gets the Redis client.- Returns:
- the Redis client
-
getDataDir
Gets the data directory path.- Returns:
- the data directory
-
recordCacheHit
public void recordCacheHit()Records a cache hit. -
recordCacheMiss
public void recordCacheMiss()Records a cache miss. -
recordApiCall
public void recordApiCall()Records an API call. -
printStatistics
public void printStatistics()Prints VCR statistics to stdout. -
getCacheHits
public long getCacheHits()Gets the cache hit count.- Returns:
- number of cache hits
-
getCacheMisses
public long getCacheMisses()Gets the cache miss count.- Returns:
- number of cache misses
-
getApiCalls
public long getApiCalls()Gets the API call count.- Returns:
- number of API calls
-