Skip to content

test: Fix flaky full-stack tests by randomizing base ports#7244

Draft
okurz wants to merge 1 commit intoos-autoinst:masterfrom
okurz:feature/randomize_ports_tests
Draft

test: Fix flaky full-stack tests by randomizing base ports#7244
okurz wants to merge 1 commit intoos-autoinst:masterfrom
okurz:feature/randomize_ports_tests

Conversation

@okurz
Copy link
Copy Markdown
Member

@okurz okurz commented Apr 2, 2026

Motivation:
Multiple full-stack tests were failing intermittently when run in parallel
due to "Address already in use" errors. They all defaulted to the same
base port (9526), causing conflicts in concurrent test environments.

Design Choices:

  • Introduced setup_random_base_port helper in OpenQA::Test::Utils
    to centralize randomization logic.
  • Applied setup_random_base_port to t/05-scheduler-full.t,
    t/25-cache-service.t, t/33-developer_mode.t,
    t/43-scheduling-and-worker-scalability.t, and t/full-stack.t.
  • Uses 20000 + int rand 10000 to pick a likely-free port range.
  • The helper uses ||= to allow overriding via environment variables.
  • Removed duplicate randomization logic and redundant comments across
    multiple test files.
  • Cleaned up BEGIN blocks and imports in full-stack tests.

Benefits:

  • Eliminates port conflicts during parallel test execution.
  • Improves test stability and reliability for CI/CD.
  • Centralizes logic for easier future maintenance and consistent
    port selection strategy.
  • Cleaner and more maintainable test code.

Motivation:
Multiple full-stack tests were failing intermittently when run in parallel
due to "Address already in use" errors. They all defaulted to the same
base port (9526), causing conflicts in concurrent test environments.

Design Choices:
- Introduced `setup_random_base_port` helper in `OpenQA::Test::Utils`
  to centralize randomization logic.
- Applied `setup_random_base_port` to `t/05-scheduler-full.t`,
  `t/25-cache-service.t`, `t/33-developer_mode.t`,
  `t/43-scheduling-and-worker-scalability.t`, and `t/full-stack.t`.
- Uses `20000 + int rand 10000` to pick a likely-free port range.
- The helper uses `||=` to allow overriding via environment variables.
- Removed duplicate randomization logic and redundant comments across
  multiple test files.
- Cleaned up `BEGIN` blocks and imports in full-stack tests.

Benefits:
- Eliminates port conflicts during parallel test execution.
- Improves test stability and reliability for CI/CD.
- Centralizes logic for easier future maintenance and consistent
  port selection strategy.
- Cleaner and more maintainable test code.
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.90%. Comparing base (437d808) to head (3fd7398).
⚠️ Report is 16 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7244   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files         419      419           
  Lines       43983    43997   +14     
=======================================
+ Hits        43942    43956   +14     
  Misses         41       41           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

# reuse it in listen to prevent race condition
#
# Potentially this approach can also be used in production code.
sub setup_random_base_port {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I'm wondering why mock_service_ports was removed: 8d7b9a5
I guess the according comment block was overlooked.

Copy link
Copy Markdown
Member Author

@okurz okurz Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh. That's why this comment block was a bit dangling :D

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.

2 participants