Skip to content

Conversation

@lijoantony
Copy link

Summary

This PR implements per-worker HTTP port allocation to enable parallel test execution with pytest-xdist, resolving "Address already in use" errors that caused 513 test failures when running tests in parallel mode.

Problem

When running tests with pytest -n auto --odoo-database test, all workers attempted to bind to the same HTTP port (8069), causing port conflicts and test failures. While database isolation worked correctly (each worker got test-gw0, test-gw1, etc.), HTTP port configuration was missing worker-specific logic.

Solution

This PR adds per-worker HTTP port allocation using a simple formula: base_port + worker_num + 1

Port Allocation Strategy

  • Main process: Port 8069 (base port)
  • Worker gw0: Port 8070 (8069 + 0 + 1)
  • Worker gw1: Port 8071 (8069 + 1 + 1)
  • Worker gw2: Port 8072 (8069 + 2 + 1)
  • etc.

Changes

  1. Added --odoo-http-port CLI option - Allows customizing the base HTTP port (default: 8069)
  2. Added _get_worker_number() helper - Parses worker IDs like "gw0", "gw1" into integers
  3. Configure ports in pytest_cmdline_main hook - Sets worker-specific port before HTTP server starts
  4. Modified _worker_db_name() context manager - Now handles both database and HTTP port allocation with proper cleanup
  5. Updated load_registry fixture - Passes config parameter to access CLI options
  6. Enhanced load_http fixture - Configures worker-specific ports for --odoo-http flag

Implementation Details

  • Mirrors the existing database isolation pattern for consistency
  • Proper cleanup: Restores original HTTP port in context manager's finally block
  • Graceful error handling: Falls back to default port if worker ID parsing fails
  • Works transparently: No changes needed to existing test code

Benefits

  • ✅ All 2,114 tests pass in parallel mode (previously 513 failed)
  • ✅ No "Address already in use" errors
  • ✅ True parallel execution enables ~14x speedup on 14-core machines
  • ✅ Backward compatible: Sequential execution unchanged
  • ✅ Configurable: Users can adjust base port if needed

Testing

Tested with:

# Parallel execution (14 workers)
pytest -n auto --odoo-database test
# Result: 2114 passed, 34 skipped in ~120s

# Custom base port
pytest -n auto --odoo-database test --odoo-http-port 9000
# Workers use ports 9001-9014

# Sequential execution (unchanged)
pytest --odoo-database test
# Uses default port 8069

Related Issues

Resolves parallel test execution failures caused by HTTP port conflicts in pytest-xdist mode.


🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

This commit implements per-worker HTTP port allocation to enable parallel
test execution with pytest-xdist, resolving "Address already in use" errors
that caused 513 test failures when running tests in parallel mode.

Changes:
- Add --odoo-http-port CLI option to customize base HTTP port (default: 8069)
- Add _get_worker_number() helper to parse worker IDs (gw0, gw1, etc.)
- Configure unique ports in pytest_cmdline_main hook before server starts
- Modify _worker_db_name() context manager to handle HTTP port allocation
- Update load_registry fixture to pass config parameter
- Enhance load_http fixture with worker-specific port configuration

Port allocation strategy:
- Main process: base_port (e.g., 8069)
- Worker gw0: base_port + 1 (e.g., 8070)
- Worker gw1: base_port + 2 (e.g., 8071)
- Worker gw2: base_port + 3 (e.g., 8072)
- etc.

The implementation mirrors the existing database isolation pattern and ensures
proper cleanup by restoring the original HTTP port in the context manager's
finally block.

This enables all 2,114 tests to pass in parallel mode (previously 513 failed).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant