Skip to content

test(apiTests): isolate per-process loopback address to prevent port collisions - #1177

Merged
kriszyp merged 4 commits into
mainfrom
kris/apitests-isolation
Jun 11, 2026
Merged

test(apiTests): isolate per-process loopback address to prevent port collisions#1177
kriszyp merged 4 commits into
mainfrom
kris/apitests-isolation

Conversation

@kriszyp

@kriszyp kriszyp commented Jun 9, 2026

Copy link
Copy Markdown
Member

Summary

  • setupTestApp.mjs acquires a unique 127.0.0.x loopback address from @harperfast/integration-testing's pool and binds all server ports (HTTP 9926, ops 9925, MQTT 1883/8883) to it
  • All 10 *-test.mjs files now import dynamic URL variables (baseUrl, wsBaseUrl, operationsUrl, mqttUrl, mqttsUrl, testHost) instead of hardcoding localhost strings
  • utility.js (CJS) reads HARPER_TEST_HOST / HARPER_TEST_OPS_PORT env vars set by setupTestApp.mjs
  • testApp/resources.js reads HARPER_TEST_HOST for the CacheOfHttp source's self-fetches

Purpose

test:unit:apitests hard-coded localhost:9926/9925/1883/8883 — running two agents simultaneously caused server startup conflicts. This gives each process its own isolated loopback address, matching how test:integration:* tests work.

Areas for attention

  • serverStarted guard: address acquisition is gated on !serverStarted, so re-entrant setupTestApp() calls (for test re-initialisation) don't acquire a second address. The setProperty calls run unconditionally, which is correct — they re-set the same host on reuse.
  • beforeExit vs process.exit(): mocha calls process.exit() explicitly, so beforeExit may not fire. The integration-testing pool uses PID liveness detection so leaks self-heal; this is a known limitation, not a defect.
  • MQTT ephemeral listeners (8884/8885): mqtt-test.mjs binds short-lived TLS listeners; changed to listen(port, testHost, callback).
  • Codex finding (addressed): Codex flagged that CacheOfHttp source in resources.js still fetched http://localhost:9926. Fixed in commit 9185ef0b1 using process.env.HARPER_TEST_HOST.
  • Gemini review: attempted but blocked by auth configuration failure in this environment (Invalid auth method selected).

Generated by Claude Sonnet 4.6

kriszyp and others added 3 commits June 8, 2026 16:49
Each mocha run now acquires a unique 127.0.0.x address from the
@harperfast/integration-testing loopback pool before starting the
in-process Harper server, so concurrent agent runs bind to different
addresses and never collide on ports 9925/9926/1883/8883.

- setupTestApp.mjs: call getNextAvailableLoopbackAddress() on first
  server start; update all setProperty() port bindings (HTTP, MQTT,
  MQTT-TLS, operations API) to use the acquired address; export
  baseUrl/wsBaseUrl/operationsUrl/mqttUrl/mqttsUrl/testHost live
  bindings so test files see the address without any re-import; release
  address on beforeExit; set HARPER_TEST_HOST env var for CJS callers
- utility.js: read HARPER_TEST_HOST / HARPER_TEST_OPS_PORT env vars so
  callOperation() reaches the correct operations API address
- All *-test.mjs files: import the relevant URL variables and replace
  every hardcoded http://localhost:9926, ws://localhost:9926,
  http://localhost:9925, mqtt://localhost:1883, mqtts://localhost:8883
  reference; also bind the ephemeral mTLS test listeners (8884/8885) to
  testHost so they follow the loopback assignment

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add missing baseUrl import to mqtt-test.mjs, remove spurious
setupTestApp import from ws-json-work.mjs, apply prettier reformats.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ches

resources.js runs inside the Harper instance and fetches back from itself
via HTTP. When the test server is bound to a loopback address other than
localhost, the hardcoded http://localhost:9926 fetch fails. Read
HARPER_TEST_HOST (set by setupTestApp) to build the correct origin.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@kriszyp
kriszyp requested review from heskew and kylebernhardy June 9, 2026 02:03
@kriszyp
kriszyp marked this pull request as ready for review June 9, 2026 02:09
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@claude

claude Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Reviewed; no blockers found.

@kriszyp
kriszyp merged commit 8cf1448 into main Jun 11, 2026
35 of 37 checks passed
@kriszyp
kriszyp deleted the kris/apitests-isolation branch June 11, 2026 04:18
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