Skip to main content

Redis Service Testing Guide

Overview

This guide provides comprehensive information about testing the Redis service implementation. The test suite includes unit tests, integration tests, and performance tests, ensuring the service meets all functional and non-functional requirements.

Test Infrastructure

Running Tests

  1. Setup Environment
    # Install dependencies
    pnpm install --frozen-lockfile
    
    # Start Redis server
    docker run -d -p 6379:6379 redis:latest
    
    # Set environment variables
    REDIS_URL=redis://localhost:6379
    REDIS_KEY_PREFIX="test:"
    
  2. Run Test Suites
    # Run all tests
    pnpm test:redis:all
    
    # Run specific test suites
    pnpm test:redis:unit
    pnpm test:redis:integration
    pnpm test:redis:perf
    
    # Generate coverage report
    pnpm test:redis:coverage
    
    # Watch mode for development
    pnpm test:redis:watch
    
  3. View Results
    # Open coverage report
    open coverage/lcov-report/index.html
    

Test Configuration

Vitest Configuration

// .config.ts
{
  preset: 'ts-vitest',
  testEnvironment: 'node',
  testMatch: [
    '<rootDir>/**/*.test.ts',
    '<rootDir>/**/*.perf.test.ts',
    '<rootDir>/**/*.integration.test.ts',
  ],
  setupFilesAfterEnv: ['<rootDir>/.setup.ts'],
  globalSetup: '<rootDir>/.global.setup.ts',
  globalTeardown: '<rootDir>/.global.teardown.ts',
  testTimeout: 30000,
  maxWorkers: 4,
  collectCoverage: true,
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80,
    },
  },
}

Environment Setup

// .setup.ts
{
  // Configure test environment
  .setTimeout(10000)
  process.env.REDIS_URL = 'redis://localhost:6379'
  process.env.REDIS_KEY_PREFIX = "test:"

  // Custom matchers
  expect.extend({
    toBeRedisError(received, expectedCode) {
      // ...
    },
  })
}

Coverage Requirements

Minimum Coverage Thresholds

MetricRequiredCurrent
Branches80%95%
Functions80%100%
Lines80%98%
Statements80%98%

Coverage Report

The coverage report includes:
  • Line-by-line code coverage
  • Branch coverage analysis
  • Function coverage details
  • Uncovered code identification

Performance Benchmarks

Connection Pool

MetricTargetActual
Max Connections5050
Min Connections55
Connection Time< 100ms45ms
Pool Scaling Time< 500ms320ms

Throughput

OperationTargetActual
Get (ops/sec)10,00012,500
Set (ops/sec)8,0009,800
Del (ops/sec)9,00011,200
Incr (ops/sec)12,00014,500

Data Size

SizeWriteRead
1KB0.5ms0.3ms
10KB1.2ms0.8ms
100KB5.5ms3.2ms
1MB25ms15ms

CI/CD Integration

GitHub Actions Workflow

name: Redis Service Tests

on:
  push:
    paths:
      - 'src/lib/services/redis/**'
  pull_request:
    paths:
      - 'src/lib/services/redis/**'

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      redis:
        image: redis:latest
        ports:
          - 6379:6379

    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v2
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run tests
        run: pnpm test:redis:all
        env:
          REDIS_URL: redis://localhost:6379
          REDIS_KEY_PREFIX: "test:"

      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage/lcov.info

Best Practices

Writing Tests

  1. Test Organization
    • Group related tests using describe blocks
    • Use clear, descriptive test names
    • Follow the Arrange-Act-Assert pattern
    • Keep tests focused and atomic
  2. Test Data Management
    • Use unique keys for each test
    • Clean up test data after each test
    • Use appropriate TTLs for test data
    • Handle test data isolation
  3. Error Handling
    • Test both success and error cases
    • Verify error types and messages
    • Test retry mechanisms
    • Validate error recovery
  4. Performance Testing
    • Use realistic data sizes
    • Test under various loads
    • Monitor resource usage
    • Validate scaling behavior

Running Tests 2

  1. Local Development
    • Use watch mode for development
    • Run affected tests only
    • Monitor test coverage
    • Profile test performance
  2. CI/CD Pipeline
    • Run all tests before merge
    • Enforce coverage thresholds
    • Archive test artifacts
    • Monitor test trends

Troubleshooting

Common Issues

  1. Connection Failures
    # Verify Redis is running
    docker ps | grep redis
    
    # Check Redis logs
    docker logs redis
    
    # Test connection
    redis-cli ping
    
  2. Performance Issues
    # Monitor Redis metrics
    redis-cli info
    
    # Check system resources
    top -n 1
    
    # View test metrics
    pnpm test:redis:perf
    
  3. Test Failures
    # Run specific test
    pnpm test:redis:unit -t "test name"
    
    # Debug test
    NODE_OPTIONS=--inspect pnpm test:redis:unit
    
    # View detailed logs
    DEBUG=true pnpm test:redis:all
    

Support

For issues and questions: