fix: avoid mock-server port races in concurrent tests (#150492)

Prevent startup port collisions in concurrent mock OpenAI tests by acquiring the port in the server process.
This commit is contained in:
Shakker 2026-09-17 03:40:33 +01:00 • committed by GitHub
parent e3e3a349b9
commit b30dc1e626
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -173,7 +173,7 @@ async function withMockServer(
},
) => Promise<void>,
) {
const port = env.MOCK_PORT === undefined ? await getFreePort() : Number(env.MOCK_PORT);
const port = Number(env.MOCK_PORT ?? "0");
let stderr = "";
let stdout = "";
const child = spawn(process.execPath, [scriptPath], {
@ -1148,8 +1148,9 @@ describe("mock OpenAI response markers", () => {
});
describe("e2e mock and config helper numeric limits", () => {
it("reports the actual OS-assigned port for MOCK_PORT=0", async () => {
await withMockServer(mockOpenAiPath, { MOCK_PORT: "0" }, async (baseUrl, output) => {
it.each([undefined, "0"])("reports the bound port for MOCK_PORT=%s", async (port) => {
const env: Record<string, string> = port === undefined ? {} : { MOCK_PORT: port };
await withMockServer(mockOpenAiPath, env, async (baseUrl, output) => {
expect(Number(new URL(baseUrl).port)).toBeGreaterThan(0);
expect(output.stdout()).not.toContain("mock-openai listening on 0\n");
const response = await fetch(`${baseUrl}/v1/responses`, {
@ -1274,6 +1275,7 @@ describe("e2e mock and config helper numeric limits", () => {
await withMockServer(
webSearchMockPath,
{
MOCK_PORT: String(await getFreePort()),
MOCK_REQUEST_LOG: requestLogDirectory,
RAW_SCHEMA_ERROR: "400 schema rejected",
SUCCESS_MARKER: "OPENCLAW_SCHEMA_E2E_OK",