Class VCRContext

java.lang.Object
com.redis.vl.test.vcr.VCRContext

public class VCRContext extends Object
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 Details

    • VCR_MODE_ENV

      public static final String VCR_MODE_ENV
      Environment variable name for overriding VCR mode.
      See Also:
  • Constructor Details

    • VCRContext

      public VCRContext(VCRTest config)
      Creates a new VCR context with the given configuration.

      The VCR mode can be overridden via the VCR_MODE environment 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

      public void initialize() throws Exception
      Initializes the VCR context, starting Redis and loading existing cassettes.
      Throws:
      Exception - if initialization fails
    • getCassetteStore

      public VCRCassetteStore 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

      public void setCurrentTest(String testId)
      Sets the current test context.
      Parameters:
      testId - the unique test identifier
    • getCurrentTestId

      public String getCurrentTestId()
      Gets the current test ID.
      Returns:
      the current test ID
    • generateCassetteKey

      public String generateCassetteKey(String type)
      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

      public List<String> getCurrentCassetteKeys()
      Gets the cassette keys generated for the current test.
      Returns:
      list of cassette keys
    • deleteCassettes

      public void deleteCassettes(List<String> keys)
      Deletes the specified cassettes.
      Parameters:
      keys - the cassette keys to delete
    • persistCassettes

      public void persistCassettes()
      Persists cassettes by triggering a Redis BGSAVE.
    • getRegistry

      public VCRRegistry getRegistry()
      Gets the VCR registry.
      Returns:
      the registry
    • getConfiguredMode

      public VCRMode getConfiguredMode()
      Gets the configured VCR mode.
      Returns:
      the configured mode
    • getEffectiveMode

      public VCRMode getEffectiveMode()
      Gets the effective VCR mode for the current test.
      Returns:
      the effective mode
    • setEffectiveMode

      public void setEffectiveMode(VCRMode mode)
      Sets the effective VCR mode.
      Parameters:
      mode - the mode to set
    • getJedis

      public UnifiedJedis getJedis()
      Gets the Redis client.
      Returns:
      the Redis client
    • getDataDir

      public Path 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