Files
core/test/router-parity/owner.test.ts
T
chrnx bca6fd09eb
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 33s
fix(app): listen() rejects when the port is taken (#138)
`listen()` awaited `server.listen(port, cb)` without an `error` listener.
A bind failure (EADDRINUSE, EACCES) never calls the callback, so the
promise hung and the error went unhandled. That is the 30 s timeout #138
saw on CI: owner.test.ts reserved a port with `freePort()` (listen on 0,
close, reuse the number), another process on the shared runner took it,
and the teapot's `listen` hung instead of failing.

- core: `bind()` rejects on the `error` event. On failure `server` goes
  back to undefined, so `close()` resolves and `listen()` can retry.
- owner.test.ts: lifetime A listens on port 0 directly, since its teapot
  is up before the teacup needs the port. Lifetime B has to boot its
  teacup against a port nobody holds yet, so it keeps `freePort()` and
  reruns on a fresh port on EADDRINUSE, up to three times.

Tests: listen on a taken port rejects with EADDRINUSE, then the same app
listens on 0 and serves; close() after a failed listen resolves. Both
hung without the fix.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 14:13:39 -05:00

296 lines
11 KiB
TypeScript

/**
* Owner-requested additions (CARD-08 review) that need a running app: cross-pattern `Allow` through
* both adapters, and the HTTP route table across a mesh peer's lifetime.
*/
import 'reflect-metadata';
import http from 'http';
import net from 'net';
import type { AddressInfo } from 'net';
import { describe, it, afterAll, beforeAll } from 'vitest';
import { createApp, Route, Get, Post, Module } from '../../src/index';
import { ALLOW_REQUESTS, CrossAllowModule, fetchRequest, stableHeaders, type Seen } from './app-fixture';
import type { App } from '../../src/app/types';
import type { Logger } from '../../src/logger';
import { frozen, expectFrozenEqual } from './frozen';
function nodeRequest(port: number, method: string, path: string): Promise<Seen> {
return new Promise((resolve, reject) => {
const req = http.request({ host: '127.0.0.1', port, method, path, agent: false }, (res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => (body += chunk));
res.on('end', () =>
resolve({ method, path, status: res.statusCode ?? 0, allow: (res.headers.allow as string) ?? null, body }),
);
});
req.on('error', reject);
req.end();
});
}
// ── mesh: the HTTP route table across a peer's lifetime ─────────────────────────────────────────
const SECRET = 'parity-secret';
@Route('/remote')
class RemoteCtl {
@Get('/ping', { export: true }) ping() {
return { from: 'teapot' };
}
}
@Module({ mountpoint: '/api', controllers: [RemoteCtl] })
class TeapotModule {}
@Route('/local')
class LocalCtl {
@Get('/ok') ok() {
return { from: 'teacup' };
}
}
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
class TeacupModule {}
const quiet: Logger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} };
/** A port nothing is listening on, that a teapot can take later. */
async function freePort(): Promise<number> {
const server = net.createServer();
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const { port } = server.address() as AddressInfo;
await new Promise<void>((resolve) => server.close(() => resolve()));
return port;
}
// Same probes at every phase: the remote hit, its method miss, a local hit and a path miss.
const PROBES: Array<[string, string]> = [
['GET', '/api/remote/ping'],
['POST', '/api/remote/ping'],
['GET', '/api/local/ok'],
['GET', '/api/nope'],
];
async function probe(app: App, phase: string) {
const out = [];
for (const [method, path] of PROBES) {
const { status, allow, body } = await fetchRequest(app, method, path);
// The teapot's ephemeral port appears in the 503 body; the port is not routing behaviour.
out.push({ phase, method, path, status, allow, body: body.replace(/ws:\/\/127\.0\.0\.1:\d+/g, '<teapot>') });
}
return out;
}
const teacupFor = (port: number) =>
createApp({
modules: [TeacupModule],
experimental: true,
logger: quiet,
mesh: {
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: SECRET }],
bootTimeoutMs: 300,
timeoutMs: 500,
// Reconnect on (CARD-19, D15): a dropped teapot is "reconnecting" and keeps its routes (A2),
// and one absent at boot is retried in the background (B). Captured at ed47836 with
// `reconnect: false`, when neither depended on it. Fast backoff keeps the test short.
reconnect: { initialDelayMs: 20, maxDelayMs: 60 },
},
});
/** Resolves on the teacup's next `mesh:routes` event: its remote table changed. */
const routesChanged = (app: App) =>
new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => reject(new Error('no mesh:routes within 5s')), 5000);
const off = app.bus.on('mesh:routes', () => {
clearTimeout(timer);
off();
resolve();
});
});
/**
* Two lifetimes. A: the teapot is up at boot, then goes away. B: the teapot is absent at boot and
* comes up afterwards. At ed47836 the HTTP table was built once, at boot, and never changed, so B2
* answered 404. Since CARD-19 (D15, owner approval) the remote routes follow membership: a teapot
* that is reconnecting keeps its routes and answers 503 (A2), and a late one is spliced in when it
* connects (B2). CARD-10 can reuse this to assert the index is not rebuilt per request.
*/
async function meshLifetimes() {
const results = [];
// Port 0: A's teapot is up before its teacup needs the port, so nothing has to be reserved.
const teapotA = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
const teacupA = teacupFor(((await teapotA.listen(0)).address() as AddressInfo).port);
try {
results.push(...(await probe(teacupA, 'A1 teapot up at boot')));
await teapotA.close();
results.push(...(await probe(teacupA, 'A2 teapot gone after boot')));
} finally {
await teacupA.close();
}
results.push(...(await lateTeapot()));
return results;
}
/**
* Lifetime B. Its teacup must know the port before the teapot exists, so the port is only free when
* checked; another process on a shared runner can take it before the teapot binds (#138). That is
* the host, not the router, so the whole lifetime reruns on a fresh port.
*/
async function lateTeapot(attempts = 3): Promise<Awaited<ReturnType<typeof probe>>> {
const portB = await freePort();
const teacupB = teacupFor(portB);
const teapotB = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
try {
const results = await probe(teacupB, 'B1 teapot absent at boot');
const spliced = routesChanged(teacupB);
try {
await teapotB.listen(portB);
} catch (err) {
spliced.catch(() => {});
if ((err as NodeJS.ErrnoException).code === 'EADDRINUSE' && attempts > 1) return await lateTeapot(attempts - 1);
throw err;
}
await spliced;
results.push(...(await probe(teacupB, 'B2 teapot up after boot')));
return results;
} finally {
await teacupB.close();
await teapotB.close();
}
}
// ── mesh: local and remote routes answering one path (CARD-07 indexes them as two trees) ─────────
@Route('/')
class RemoteMixCtl {
// Same path as a local GET, another method.
@Post('/dup/ping', { export: true }) dupPost() {
return { from: 'teapot', route: 'POST /api/dup/ping' };
}
// A remote param route overlapping a local static one.
@Post('/v/:id', { export: true }) vPost() {
return { from: 'teapot', route: 'POST /api/v/:id' };
}
// Remote only: remote routes are `buffer`, so the teacup's router resolves HEAD to it implicitly.
// Captured at ed47836 as HEAD → 404 (the teacup forwarded HEAD, and the teapot has no HEAD plan).
// CARD-20 (#125, D16) changed that deliberately, with owner approval: the teacup forwards the
// HEAD as a GET and drops the body, so HEAD now answers 200 like a local route.
@Get('/x', { export: true }) x() {
return { from: 'teapot', route: 'GET /api/x' };
}
}
@Module({ mountpoint: '/api', controllers: [RemoteMixCtl] })
class RemoteMixTeapotModule {}
@Route('/')
class LocalMixCtl {
@Get('/dup/ping') dupGet() {
return { from: 'teacup', route: 'GET /api/dup/ping' };
}
@Get('/v/b') vGet() {
return { from: 'teacup', route: 'GET /api/v/b' };
}
}
@Module({ mountpoint: '/api', controllers: [LocalMixCtl] })
class LocalMixTeacupModule {}
const MIX_REQUESTS: Array<[string, string]> = [
...['PUT', 'OPTIONS', 'HEAD', 'GET', 'POST'].map((m): [string, string] => [m, '/api/dup/ping']),
...['PUT', 'OPTIONS', 'HEAD', 'GET', 'POST'].map((m): [string, string] => [m, '/api/v/b']),
...['PUT', 'OPTIONS', 'HEAD', 'GET'].map((m): [string, string] => [m, '/api/v/c']),
...['HEAD', 'GET', 'OPTIONS', 'PUT'].map((m): [string, string] => [m, '/api/x']),
];
/** Full stable headers for both adapters, so HEAD body suppression and content headers are visible. */
async function mixedTable() {
const teapot = createApp({ modules: [RemoteMixTeapotModule], experimental: true, mesh: { secret: SECRET } });
const tServer = await teapot.listen(0);
const url = `ws://127.0.0.1:${(tServer.address() as AddressInfo).port}/__mesh__/control`;
const teacup = createApp({
modules: [LocalMixTeacupModule],
experimental: true,
logger: quiet,
mesh: { teapots: [{ url, secret: SECRET }], reconnect: false },
});
try {
const port = ((await teacup.listen(0)).address() as AddressInfo).port;
const out = [];
for (const [method, path] of MIX_REQUESTS) {
const res = await teacup.fetch(new Request(`http://parity.test${path}`, { method }));
out.push({
adapter: 'fetch',
method,
path,
status: res.status,
headers: stableHeaders(res.headers),
body: await res.text(),
});
}
for (const [method, path] of MIX_REQUESTS) {
const seen = await nodeRequestWithHeaders(port, method, path);
out.push({ adapter: 'node', method, path, ...seen });
}
return out;
} finally {
await teacup.close();
await teapot.close();
}
}
function nodeRequestWithHeaders(port: number, method: string, path: string) {
return new Promise<{ status: number; headers: Record<string, string>; body: string }>((resolve, reject) => {
const req = http.request({ host: '127.0.0.1', port, method, path, agent: false }, (res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => (body += chunk));
res.on('end', () => {
const headers = Object.entries(res.headers).map(([k, v]): [string, string] => [
k,
Array.isArray(v) ? v.join(', ') : String(v),
]);
resolve({ status: res.statusCode ?? 0, headers: stableHeaders(headers), body });
});
});
req.on('error', reject);
req.end();
});
}
describe('router parity: owner-requested application cases', () => {
let fetchApp: App;
let nodeApp: App;
let port: number;
beforeAll(async () => {
fetchApp = createApp({ modules: [CrossAllowModule] });
nodeApp = createApp({ modules: [CrossAllowModule] });
port = ((await nodeApp.listen(0)).address() as AddressInfo).port;
});
afterAll(() => nodeApp.close());
it('cross-pattern Allow through app.fetch and Node equals the frozen capture', async () => {
const run = async () => {
const out: Array<Seen & { adapter: string }> = [];
for (const [method, path] of ALLOW_REQUESTS)
out.push({ adapter: 'fetch', ...(await fetchRequest(fetchApp, method, path)) });
for (const [method, path] of ALLOW_REQUESTS)
out.push({ adapter: 'node', ...(await nodeRequest(port, method, path)) });
return out;
};
const expected = await frozen('owner-app-allow', run);
expectFrozenEqual(await run(), expected, 'cross-pattern Allow');
});
it('the mesh HTTP route table across a peer lifetime equals the frozen capture', async () => {
const expected = await frozen('owner-mesh-lifetime', meshLifetimes);
expectFrozenEqual(await meshLifetimes(), expected, 'mesh lifetime');
}, 30_000);
it('local and remote routes on one path: Allow union, precedence and remote implicit HEAD equal the frozen capture', async () => {
const expected = await frozen('owner-mesh-allow', mixedTable);
expectFrozenEqual(await mixedTable(), expected, 'mesh local/remote mix');
}, 30_000);
});