Skip to content

Commit a34fa7d

Browse files
fix(gateway): resolve flaky tests from port conflicts and shared state (#75)
* fix(gateway): use OS-assigned port in app.test.ts to avoid EADDRINUSE Fixes #72 The integration tests in app.test.ts were using the default port 3100, which conflicts with sdk.integration.test.ts when running in parallel. Changes: - Set PORT=0 in vi.hoisted block to get an OS-assigned port - Add afterAll hook to properly close the server after tests - Import server export for cleanup * fix(gateway): isolate tests with shared state using forks pool Fixes #70 The concurrency module has shared state that was causing "socket hang up" errors when concurrency.test.ts ran in parallel with integration tests. Changes: - Use poolMatchGlobs to run integration and concurrency tests in forks pool - Other tests continue to run in parallel threads pool for speed - Forks pool provides process isolation, preventing shared state conflicts
1 parent 9ed751e commit a34fa7d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

packages/gateway/__tests__/integration/app.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@
77

88
import { spawn } from "node:child_process";
99
import request from "supertest";
10-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10+
import {
11+
afterAll,
12+
afterEach,
13+
beforeEach,
14+
describe,
15+
expect,
16+
it,
17+
vi,
18+
} from "vitest";
1119
import {
1220
afterSpawnCalled,
1321
createCliResultJson,
1422
createMockChildProcess,
1523
} from "../helpers.js";
1624

17-
// Set required environment variable BEFORE any imports
25+
// Set required environment variables BEFORE any imports
1826
// Using vi.hoisted to ensure this runs at the earliest possible time
1927
const TEST_API_KEY = vi.hoisted(() => {
2028
const key = "test-api-key-12345";
2129
process.env.CLAUDE_CODE_GATEWAY_API_KEY = key;
30+
// Use port 0 to let OS assign an available port (avoids conflicts in parallel runs)
31+
process.env.PORT = "0";
2232
return key;
2333
});
2434

@@ -29,12 +39,19 @@ vi.mock("node:child_process", () => ({
2939

3040
const mockSpawn = vi.mocked(spawn);
3141

32-
// Import app after env var is set
33-
import app from "../../src/index.js";
42+
// Import app and server after env var is set
43+
import app, { server } from "../../src/index.js";
3444

3545
describe("Claude Code Wrapper App (Integration)", () => {
3646
const validAuthHeader = `Bearer ${TEST_API_KEY}`;
3747

48+
afterAll(async () => {
49+
// Close the server to prevent resource leaks and port conflicts
50+
await new Promise<void>((resolve, reject) => {
51+
server.close((err) => (err ? reject(err) : resolve()));
52+
});
53+
});
54+
3855
beforeEach(() => {
3956
vi.clearAllMocks();
4057
});

packages/gateway/vitest.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ export default defineConfig({
88
setupFiles: ["./__tests__/setup.ts"],
99
include: ["__tests__/**/*.test.ts"],
1010
exclude: ["__tests__/e2e/**/*.test.ts"],
11+
// Use forks pool for tests with shared state (concurrency module, server ports)
12+
// to isolate them from parallel execution. Other tests run in threads pool.
13+
// This fixes flaky tests from #70 and #72.
14+
pool: "threads",
15+
poolMatchGlobs: [
16+
// Integration tests start actual servers and need isolation
17+
["**/__tests__/integration/**", "forks"],
18+
// Concurrency tests modify shared module state
19+
["**/__tests__/concurrency.test.ts", "forks"],
20+
],
1121
coverage: {
1222
provider: "v8",
1323
reporter: ["text", "lcov"],

0 commit comments

Comments
 (0)