This document describes the automated testing infrastructure created to verify RQ worker file processing and cleanup code.
The testing suite verifies that:
- File processing jobs execute correctly through RQ workers
- Database session cleanup (
Session.remove()) is properly called in finally blocks - Embedding function cleanup (
EMBEDDING_FUNCTION = None) is performed after jobs - Resources are properly released after job completion or failure
Location: backend/open_webui/test/apps/webui/workers/test_file_processor.py
Features:
- Unit tests for cleanup verification
- Mock-based tests (no Redis/DB required)
- Integration tests for code structure verification
- Resource leak detection tests
Test Categories:
TestFileProcessorCleanup: Tests cleanup execution on success and error pathsTestFileProcessorIntegration: Tests code structure and patternsTestCleanupResourceLeaks: Tests cleanup across multiple jobs
Location: test_worker_cleanup_verification.py
Features:
- Quick verification without pytest
- Code structure verification
- Mock execution tests
- Optional Redis/DB connection tests
Location: backend/open_webui/test/apps/webui/workers/README.md
Content:
- Usage instructions
- Test categories explained
- Environment variable documentation
- Troubleshooting guide
# Activate conda environment
conda activate rit4test
# Option 1: Run pytest tests (recommended)
pytest backend/open_webui/test/apps/webui/workers/test_file_processor.py -v
# Option 2: Run standalone verification script
python test_worker_cleanup_verification.pyIf you have Redis and PostgreSQL running:
# Set environment variables
export REDIS_URL=redis://localhost:6379
export DATABASE_URL=postgresql://user:pass@localhost:5432/dbname
export ENABLE_JOB_QUEUE=true
# Run tests
pytest backend/open_webui/test/apps/webui/workers/test_file_processor.py -vVerifies:
Session.remove()is called in the finally block- Cleanup happens on successful job completion
- Cleanup happens even when job fails with exception
- Multiple jobs each trigger cleanup independently
Test Methods:
test_session_cleanup_called_on_successtest_session_cleanup_called_on_errortest_multiple_jobs_cleanup
Verifies:
EMBEDDING_FUNCTIONis set toNoneafter job completion- Per-job resources are properly released
- Cleanup pattern is correct in code structure
Test Methods:
test_embedding_function_cleanuptest_mock_request_cleanup_pattern
Verifies:
finally:block exists inprocess_file_jobSession.remove()is in the finally blockEMBEDDING_FUNCTIONcleanup is in the finally block- Proper imports are present
Test Methods:
test_file_processing_job_structuretest_session_import
pytest backend/open_webui/test/apps/webui/workers/test_file_processor.py -vpytest backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorCleanup -vpytest backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorCleanup::test_session_cleanup_called_on_success -vpytest backend/open_webui/test/apps/webui/workers/test_file_processor.py \
--cov=open_webui.workers.file_processor \
--cov-report=html \
--cov-report=termpython test_worker_cleanup_verification.py================================================================================
RQ Worker File Processing - Cleanup Verification Tests
================================================================================
Python: 3.x.x
Working directory: /path/to/project
Backend path: /path/to/project/backend
================================================================================
TEST 1: Verifying Cleanup Code Structure
================================================================================
✅ Found 'finally:' block
✅ Found Session.remove() in finally block (line 1428)
✅ Found EMBEDDING_FUNCTION cleanup in finally block (line 1439)
✅ All cleanup code structure checks passed
================================================================================
TEST 2: Verifying Session Import
================================================================================
✅ Session imported and has remove() method
✅ Session import verified in file_processor.py
================================================================================
TEST 3: Verifying Cleanup Execution (Mocked)
================================================================================
✅ Session.remove() was called in finally block
================================================================================
TEST SUMMARY
================================================================================
✅ PASS: Cleanup Code Structure
✅ PASS: Session Import
✅ PASS: Cleanup Execution (Mocked)
✅ PASS: End-to-End with Redis (Optional)
================================================================================
Results: 4/4 tests passed
✅ ALL TESTS PASSED - Cleanup code is properly implemented!
backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorCleanup::test_session_cleanup_called_on_success PASSED
backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorCleanup::test_session_cleanup_called_on_error PASSED
backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorCleanup::test_embedding_function_cleanup PASSED
backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorIntegration::test_file_processing_job_structure PASSED
backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestFileProcessorIntegration::test_mock_request_cleanup_pattern PASSED
backend/open_webui/test/apps/webui/workers/test_file_processor.py::TestCleanupResourceLeaks::test_multiple_jobs_cleanup PASSED
======================== 6 passed in 2.34s ========================
The tests use these environment variables (optional):
REDIS_URL- Redis connection URL (for integration tests)DATABASE_URL- PostgreSQL connection URL (for integration tests)ENABLE_JOB_QUEUE- Enable job queue (default: False)RAG_EMBEDDING_ENGINE- Embedding engine configurationRAG_EMBEDDING_MODEL- Embedding model configuration
Note: Most tests use mocks and don't require actual Redis/DB connections. Only integration tests (marked with @pytest.mark.integration) require these.
Most tests use Python's unittest.mock to:
- Mock database operations (Files, Users models)
- Mock storage operations (Storage.get_file)
- Mock vector DB operations (VECTOR_DB_CLIENT)
- Mock embedding function calls
- Track cleanup method calls
This allows tests to:
- Run without Redis/DB setup
- Run quickly
- Verify cleanup logic without side effects
- Test error paths safely
- Patch and Assert: Patch
Session.remove()and assert it was called - Source Code Inspection: Parse source code to verify structure
- Log Monitoring: Capture and verify cleanup log messages
- Resource Tracking: Track resource usage across multiple jobs
Problem: ModuleNotFoundError: No module named 'open_webui'
Solution:
- Ensure you're in the project root directory
- Verify backend is in Python path
- Activate conda environment:
conda activate rit4test - Install dependencies:
pip install -r backend/requirements.txt
Problem: Redis connection failures in tests
Solution:
- These are expected if Redis is not running
- Tests will skip Redis-dependent tests automatically
- For full integration testing, start Redis:
docker run -d -p 6379:6379 redis
Problem: Database connection failures
Solution:
- Most tests use mocks and don't need a database
- Only integration tests need a real database
- Set
DATABASE_URLenvironment variable if running integration tests
Problem: Tests fail with "Session.remove() was NOT called"
Solution:
- Verify cleanup code exists in
backend/open_webui/workers/file_processor.py - Check that finally block contains
Session.remove() - Ensure imports are correct:
from open_webui.internal.db import Session
To integrate these tests into CI/CD:
# Example GitHub Actions workflow
- name: Test RQ Worker Cleanup
run: |
conda activate rit4test
pytest backend/open_webui/test/apps/webui/workers/test_file_processor.py -vOr with Docker:
- name: Test RQ Worker Cleanup
run: |
docker-compose up -d redis postgres
pytest backend/open_webui/test/apps/webui/workers/test_file_processor.py -v-
Run the tests to verify cleanup code works:
conda activate rit4test pytest backend/open_webui/test/apps/webui/workers/test_file_processor.py -v
-
Review test output to ensure all tests pass
-
Run standalone verification for quick check:
python test_worker_cleanup_verification.py
-
Monitor in production to ensure cleanup prevents resource leaks
✅ Comprehensive test suite created ✅ Cleanup verification implemented ✅ Mock-based testing (no external dependencies required) ✅ Documentation and usage guides provided ✅ Ready for CI/CD integration
The testing infrastructure is ready to use and will help ensure that cleanup code prevents resource leaks in RQ worker file processing!