Fix flaky Cloudflare mock startup - #768
Conversation
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
📝 WalkthroughWalkthroughCloudflare mock startup now derives its origin from Wrangler output, validates readiness, detects early process exits, and uses dynamic localhost ports. The email and Artifacts mock tests now have explicit 75-second timeouts. ChangesCloudflare mock startup
Extended mock-dependent test timeouts
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant startCloudflareMock
participant wrangler_dev
participant waitForCloudflareMock
startCloudflareMock->>wrangler_dev: spawn with dynamic localhost ports
wrangler_dev-->>waitForCloudflareMock: emit ready origin
waitForCloudflareMock->>wrangler_dev: request /__mocks/meta
wrangler_dev-->>waitForCloudflareMock: readiness response or process exit
waitForCloudflareMock-->>startCloudflareMock: return origin or startup error
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
🔎 Preview deployed: https://kody-pr-768.kody-a99.workers.dev Worker: Mocks:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/worker/src/test-support/cloudflare-mock-server.ts`:
- Around line 28-39: Update the metadata probe loop around fetch in the mock
server startup flow to use a per-attempt AbortController signal bounded by the
remaining 60-second deadline, so a nonresponsive socket cannot stall startup;
pass the signal to fetch, and ensure response.body is canceled in a finally
block for every completed attempt while preserving retries for failures and
non-OK responses.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: be835460-d2fe-4adf-a356-7d2700b1ffff
📒 Files selected for processing (3)
packages/worker/src/app/email/cloudflare-email.node.test.tspackages/worker/src/repo/artifacts-mock-cloudflare.node.test.tspackages/worker/src/test-support/cloudflare-mock-server.ts
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
Summary
Root cause
get-portonly proved a port was free and then released it. Duringnpm run validate, unit, Playwright, and MCP suites run concurrently and can start competing Wrangler processes, so another process could bind either selected port before this helper's Wrangler did. The helper then discarded Wrangler's output and polled the dead origin for 25 seconds, producing onlymock cloudflare timeout.Wrangler now binds port
0atomically and reports the assigned origin. Its CI output includes ANSI control sequences inside the URL, so the helper strips those sequences before parsing the readiness line. Each metadata probe is bounded to one second (or the remaining startup budget) so a half-open response cannot bypass the overall deadline.Test evidence
FORCE_COLOR=3): 2/2 passed after reproducing the pre-fix timeoutnpm run validate: passed (263 unit files / 864 unit tests, 18 Playwright tests, MCP suite, typecheck, lint, and formatting)System recap — composes existing primitives (low risk)
Mode: recap · Base:
main@75cd1b6c· Head:2eda3994Classification: composes — this PR changes only the local test harness and test time budgets; no production primitive behavior or contract changes.
Primitives touched
artifacts-reposSystem map
The focused integration test continues to exercise the existing Artifacts repository contract through a more reliable Wrangler mock startup path.
Legend: green = composes (wiring only) · amber = extended by this PR · red = new primitive · gray = context (unchanged, included only when an edge crosses it).
Summary by CodeRabbit