Skip to content

fix(mock): avoid cluster HTTP port collisions in parallel tests - #6069

Merged
killagu merged 2 commits into
mainfrom
fix/mock-cluster-port-allocation
Oct 7, 2026
Merged

killagu merged 2 commits into
mainfrom
fix/mock-cluster-port-allocation

Conversation

@killagu

@killagu killagu commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Select the default mock cluster HTTP port with detectPort() in the child before startup, instead of a PID-based global counter. Vitest threads share a PID but have independent globals, so concurrent clusters can receive the same counter value.
  • Update app.port from egg-ready before resolving readiness, keeping address(), HTTP requests and synchronous mock RPC on the actual port. Explicit ports, including 0 as the application's configured port, keep their existing semantics.
  • Add Windows-enabled regression coverage for an occupied legacy counter port, concurrent clusters, explicit ports, configured port zero and worker-thread startup. Each cluster gets an isolated fixture directory.

Motivation

The Windows Node 22 main failure shows EADDRINUSE :17773 and a cluster startup exit, followed by customDirectory finding no schedule logs on all retries. The log does not directly map that process to the fixture, so the historical attribution remains an inference. The new negative regression deterministically reproduces the port collision and readiness continuing after startup failure on unchanged main, then passes with this fix.

The schedule test and its timing are unchanged. This uses the existing free-port detection convention; it does not claim to eliminate the general probe/release/bind race. A default app.port is now 0 until await app.ready() completes.

Validation

Linux, Node 24.19.0, no build before tests, retries disabled:

  • Mock suite: 205 passed, 61 skipped
  • Schedule suite plus worker-protocol/worker-thread-utils: 58 passed, 13 skipped
  • Final isolated port regressions: 5/5 passed
  • Occupied-port regression against main: fails with EADDRINUSE and the expected port assertion
  • Changed-file oxlint --type-aware --type-check, oxfmt --check, and git diff --check: passed
  • Full-repository lint: the same 7 unrelated diagnostics on main and this patch (missing tegg workspace dependencies/types and an existing example type-depth error)

Local environment limits: utoo registry connections timed out, so dependencies were installed with a temporary pnpm workspace derived from the tracked catalogs. The container blocks os.networkInterfaces(), so runtime tests used a local loopback-only interface shim. Neither workaround is committed; local results are not a substitute for Windows CI. The ci:full matrix has now validated the final head on all supported combinations (see below).

Final CI verification

Head ec36b5c7c884a64aee4a95ba48fa7e0062d1f605, tested as merge commit 5eab766e0bd27d97e2865e50b206deebd64fdd09 with main a9deafba1fc2bf6b937811238f9675411d66ed38:

  • CI run 37560885535: successful, all nine OS/Node combinations, static checks/builds, coverage and aggregate gate
  • Windows Node 22 job: 3,466 tests passed, 517 skipped; all five new port regressions and the original customDirectory test passed without retry markers
  • E2E run 37560885510: all three jobs passed

Summary by CodeRabbit

  • Bug Fixes
    • Mock clusters now select available ports when none are configured, avoiding conflicts with ports already in use.
    • Explicit port settings, including 0, are respected, and clusters report the port they actually use.
  • Improvements
    • A port no longer needs to be provided when configuring a mock cluster.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b389fd32-b2d0-448a-b47d-b1546c0e9da3
📥 Commits

Reviewing files that changed from the base of the PR and between a9deafb and ec36b5c.

📒 Files selected for processing (4)
  • plugins/mock/src/lib/cluster.ts
  • plugins/mock/src/lib/start-cluster.ts
  • plugins/mock/src/lib/types.ts
  • plugins/mock/test/cluster-port.test.ts
💤 Files with no reviewable changes (1)
  • plugins/mock/src/lib/types.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Cluster startup now detects a free port when no port is configured. The cluster records the actual port reported by the child process. The updated tests cover default and explicit ports, port 0, concurrent clusters, and worker-threads mode.

Changes

Mock cluster port selection

Layer / File(s) Summary
Resolve and report cluster ports
plugins/mock/src/lib/types.ts, plugins/mock/src/lib/start-cluster.ts, plugins/mock/src/lib/cluster.ts, plugins/mock/test/cluster-port.test.ts
The options type no longer requires a port. Startup detects a free port for nullish options, while preserving explicit values. ClusterApplication records the port from the egg-ready message. Tests cover port selection and address behavior across the listed configurations.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to ec36b

Default-port selection and reporting show no established merge-blocking regression. The explicit-zero edge case remains outside this change.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: preventing mock-cluster HTTP port collisions in parallel tests.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@killagu killagu added the ci:full Run the full CI compatibility matrix on the next PR run label Oct 7, 2026 — with ChatGPT Codex Connector
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Deploying egg with  Cloudflare Pages  Cloudflare Pages

Latest commit: ec36b5c
Status: ✅  Deploy successful!
Preview URL: https://155bbc8c.egg-cci.pages.dev
Branch Preview URL: https://fix-mock-cluster-port-alloca.egg-cci.pages.dev

View logs

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Deploying egg-v3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: ec36b5c
Status: ✅  Deploy successful!
Preview URL: https://267adf43.egg-v3.pages.dev
Branch Preview URL: https://fix-mock-cluster-port-alloca.egg-v3.pages.dev

View logs

@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.61%. Comparing base (a9deafb) to head (ec36b5c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6069      +/-   ##
==========================================
+ Coverage   80.39%   83.61%   +3.21%     
==========================================
  Files          18      731     +713     
  Lines         852    22608   +21756     
  Branches      228     4554    +4326     
==========================================
+ Hits          685    18903   +18218     
- Misses        139     3202    +3063     
- Partials       28      503     +475     

☔ View full report in Codecov by Harness.
📢 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.

@killagu
killagu marked this pull request as ready for review October 7, 2026 02:18
Copilot AI balanced review requested due to automatic review settings October 7, 2026 02:18

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@killagu
killagu merged commit e701300 into main Oct 7, 2026
39 checks passed
@killagu
killagu deleted the fix/mock-cluster-port-allocation branch October 7, 2026 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:full Run the full CI compatibility matrix on the next PR run

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants