|
| 1 | +# Parallel Backend Testing Setup |
| 2 | + |
| 3 | +This document explains the parallel test execution setup for the LCFS backend tests. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The backend tests have been configured to run in parallel using GitHub Actions matrix strategy. This reduces test execution time from ~15-20 minutes to ~3-5 minutes by running tests across 6 parallel jobs. |
| 8 | + |
| 9 | +## How It Works |
| 10 | + |
| 11 | +### Job Distribution |
| 12 | + |
| 13 | +Tests are distributed across 6 parallel jobs by domain/business functionality: |
| 14 | + |
| 15 | +1. **Job 1**: `compliance_report` (10 test files - largest module) |
| 16 | +2. **Job 2**: `fuel_supply,final_supply_equipment,allocation_agreement` (15 files) |
| 17 | +3. **Job 3**: `fuel_export,fuel_code,other_uses,notional_transfer` (16 files) |
| 18 | +4. **Job 4**: `services,user,transfer,transaction` (12 files) |
| 19 | +5. **Job 5**: `organization,organization_snapshot,notification,initiative_agreement` (9 files) |
| 20 | +6. **Job 6**: `credit_ledger,calculator,audit_log,organizations,internal_comment,document,admin_adjustment` (remaining files) |
| 21 | + |
| 22 | +### Database Isolation |
| 23 | + |
| 24 | +Each parallel job uses its own isolated database: |
| 25 | +- Job 1: `lcfs_test_job_1` |
| 26 | +- Job 2: `lcfs_test_job_2` |
| 27 | +- Job 3: `lcfs_test_job_3` |
| 28 | +- Job 4: `lcfs_test_job_4` |
| 29 | +- Job 5: `lcfs_test_job_5` |
| 30 | +- Job 6: `lcfs_test_job_6` |
| 31 | + |
| 32 | +This prevents conflicts between tests running simultaneously. |
| 33 | + |
| 34 | +## Configuration Files |
| 35 | + |
| 36 | +### GitHub Actions Workflow |
| 37 | +- **File**: `.github/workflows/backend-tests.yml` |
| 38 | +- **Purpose**: Defines the parallel job matrix and execution strategy |
| 39 | +- **Services**: PostgreSQL, Redis, RabbitMQ (each job gets isolated access) |
| 40 | + |
| 41 | +### Backend Configuration |
| 42 | +- **File**: `backend/lcfs/conftest.py` |
| 43 | +- **Changes**: Modified `_engine` fixture to support dynamic database names |
| 44 | +- **Environment**: Uses `LCFS_DB_BASE` environment variable for database naming |
| 45 | + |
| 46 | +### Pytest Configuration |
| 47 | +- **File**: `backend/pyproject.toml` |
| 48 | +- **Changes**: Updated pytest options for better parallel execution |
| 49 | +- **Discovery**: Enhanced test discovery patterns |
| 50 | + |
| 51 | +## Local Development |
| 52 | + |
| 53 | +### Running Tests Locally (Sequential) |
| 54 | +```bash |
| 55 | +cd backend |
| 56 | +poetry run pytest lcfs/tests/ |
| 57 | +``` |
| 58 | + |
| 59 | +### Testing Parallel Setup Locally |
| 60 | +```bash |
| 61 | +# Test a specific group with custom database name |
| 62 | +export LCFS_DB_BASE=lcfs_test_local_1 |
| 63 | +cd backend |
| 64 | +poetry run pytest lcfs/tests/compliance_report/ -v |
| 65 | +``` |
| 66 | + |
| 67 | +### Testing Multiple Groups Locally |
| 68 | +```bash |
| 69 | +# Simulate parallel execution (requires multiple terminal windows) |
| 70 | +# Terminal 1: |
| 71 | +export LCFS_DB_BASE=lcfs_test_local_1 |
| 72 | +poetry run pytest lcfs/tests/compliance_report/ |
| 73 | + |
| 74 | +# Terminal 2: |
| 75 | +export LCFS_DB_BASE=lcfs_test_local_2 |
| 76 | +poetry run pytest lcfs/tests/fuel_supply/ |
| 77 | +``` |
| 78 | + |
| 79 | +## GitHub Actions Usage |
| 80 | + |
| 81 | +### Triggering Parallel Tests |
| 82 | +The workflow automatically triggers on: |
| 83 | +- Push to `main`, `develop`, or `release-*` branches (when backend files change) |
| 84 | +- Pull requests to `main`, `develop`, or `release-*` branches (when backend files change) |
| 85 | + |
| 86 | +### Monitoring Execution |
| 87 | +1. Go to GitHub Actions tab in the repository |
| 88 | +2. Look for "Backend Tests (Parallel)" workflow |
| 89 | +3. Each job shows individual progress and logs |
| 90 | +4. Test results are aggregated in the final summary |
| 91 | + |
| 92 | +### Adding New Test Modules |
| 93 | +When adding new test modules: |
| 94 | + |
| 95 | +1. Create test directory: `backend/lcfs/tests/new_module/` |
| 96 | +2. Add test files following naming convention: `test_*.py` |
| 97 | +3. Update workflow grouping in `.github/workflows/backend-tests.yml` if needed |
| 98 | +4. Tests will be automatically discovered and executed |
| 99 | + |
| 100 | +## Performance Benefits |
| 101 | + |
| 102 | +- **Execution Time**: Reduced from 15-20 minutes to 3-5 minutes |
| 103 | +- **Parallel Jobs**: 6 simultaneous executions |
| 104 | +- **Speedup Factor**: ~4-5x improvement |
| 105 | +- **Resource Usage**: Better CI resource utilization |
| 106 | + |
| 107 | +## Troubleshooting |
| 108 | + |
| 109 | +### Database Connection Issues |
| 110 | +- Check PostgreSQL service is running in CI |
| 111 | +- Verify database permissions for test user |
| 112 | +- Ensure each job has unique database name |
| 113 | + |
| 114 | +### Test Failures in Parallel Mode |
| 115 | +- Check for shared state dependencies between tests |
| 116 | +- Verify test isolation (no cross-test data dependencies) |
| 117 | +- Review database rollback behavior in fixtures |
| 118 | + |
| 119 | +### Job Imbalance |
| 120 | +- Monitor job execution times in GitHub Actions |
| 121 | +- Redistribute test groups if some jobs take significantly longer |
| 122 | +- Consider test complexity when grouping modules |
| 123 | + |
| 124 | +## Migration from Sequential to Parallel |
| 125 | + |
| 126 | +### Backward Compatibility |
| 127 | +- Local development remains unchanged (runs sequentially by default) |
| 128 | +- Existing test commands continue to work |
| 129 | +- No changes required to individual test files |
| 130 | + |
| 131 | +### Environment Variables |
| 132 | +- `LCFS_DB_BASE`: Database name (defaults to `lcfs_test`) |
| 133 | +- `APP_ENVIRONMENT`: Always set to `pytest` for tests |
| 134 | +- Other settings remain unchanged |
| 135 | + |
| 136 | +## Future Enhancements |
| 137 | + |
| 138 | +### Phase 2 Optimizations |
| 139 | +- Add pytest-xdist for within-job parallelization |
| 140 | +- Implement UUID-based IDs to eliminate sequence conflicts |
| 141 | +- Add job-level retry logic for transient failures |
| 142 | +- Optimize fixture setup for faster database operations |
| 143 | + |
| 144 | +### Monitoring |
| 145 | +- Add execution time tracking per job |
| 146 | +- Implement test result analytics |
| 147 | +- Monitor for test flakiness in parallel mode |
0 commit comments