Files
core/test/bun/mesh-reconnect.test.ts
T
chrnx 6bbd78fc8e feat(mesh): remote routes follow teapot membership (CARD-19, #124)
The teacup built its route table once at boot. A teapot that started
late was never served, and one that was gone for good stayed advertised.
Implement D15 (accepted 2026-10-18):

- A teapot that missed bootTimeoutMs is retried in the background with
  the reconnect backoff (not with reconnect: false). On connect its
  routes join the remote table. Its steps do not, because the graph
  was validated at boot without them.
- While a link is reconnecting, its routes stay and answer 503, now
  with retry-after (seconds until the next attempt).
- A link that gives up for good (dropped with reconnect: false, or a
  permanent refusal while reconnecting) has its routes removed: 404,
  and Allow drops its methods. A refused reconnect no longer retries
  forever.
- A returning manifest replaces the teapot's routes. missingFromManifest
  now checks steps only, so a dropped route is removed instead of
  refusing the reconnect.
- A late route that conflicts with another teapot's is refused and
  logged. The table keeps serving. At boot the same conflict still
  fails boot.
- Each change emits mesh:routes with { url, added, removed }.
- The reserved-path check from CARD-21/CARD-17 (#141, #145) runs in
  the table for every remote route: at boot a route on /__mesh__/control
  (or /__live__ and /__ready__ with health on) fails boot as before. A
  late or returning one is refused and logged, and the teapot's other
  routes are served.

Mechanics: the remote table publishes a new array per change
(copy-on-write). app.fetch and listen() share one table through a
getter, and buildFetch/createHttpServer read it once per request, so a
request sees the old table or the new one, never a mix. The router
reuses local trees when the local entries are identical, so a
membership change rebuilds only the remote trees. Dev routes now sit
before remote ones in the table to keep the local positions stable.

Tests cover T1-T8 on Node (test/mesh/membership.test.ts), plus the
local-tree build counter and late reserved-path routes, and T1/T2 on
Deno and Bun.

The frozen owner-mesh-lifetime.json capture changes in the next commit.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-23 01:04:39 -05:00

190 lines
5.8 KiB
TypeScript

// Reconnection under the real Bun runtime.
//
// Separate from the Node and Deno suites deliberately. Bun's WebSocket lifecycle is a third
// implementation — its socket events land on a server-level handler rather than the socket — and a
// link that swaps its socket underneath a live graph is precisely where three implementations
// stop behaving the same. Defining this on Node and hoping is not enough.
//
// Run with: npm run test:bun
import 'reflect-metadata';
import { test, expect } from 'bun:test';
import { createApp, Step, Route, Get, Module, needs } from '../../src/index.ts';
import { serveBun } from '../../src/bun.ts';
const SECRET = 's3cr3t';
@Step({ provides: 'config', needs: [], export: true })
class Config {
run() {
return { config: { runtime: 'bun' } };
}
}
@Step({ provides: 'auth', needs: [], export: true })
class Auth {
run(ctx: any) {
return { auth: { token: ctx.headers?.['x-token'] ?? 'anon' } };
}
}
@Module({ mountpoint: '/api', steps: [Config, Auth] })
class TeapotModule {}
@Route('/local')
class LocalCtl {
@Get('/who')
who(@needs('config') config: any, @needs('auth') auth: any) {
return { config, auth };
}
}
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
class TeacupModule {}
/** A port nobody is on, so the teapot can be killed and brought back at the same address. */
function freePort(): number {
const probe = Bun.serve({ port: 0, fetch: () => new Response('') });
const { port } = probe;
probe.stop(true);
return port;
}
/** Boot a teapot on a fixed port; the handle kills it and leaves the port free again. */
async function startTeapot(port: number) {
const app = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
const server = await serveBun(app, { port });
return {
stop: async () => {
server.stop(true);
await app.close();
},
};
}
const settle = (ms: number) => new Promise((r) => setTimeout(r, ms));
test('mesh on Bun: a teacup reconnects to a teapot that came back', async () => {
const port = freePort();
let teapot = await startTeapot(port);
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: {
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: SECRET }],
reconnect: { initialDelayMs: 20, maxDelayMs: 60 },
heartbeatMs: 50,
timeoutMs: 2000,
},
});
try {
const before = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'abc' } }));
expect(before.status).toBe(200);
await teapot.stop();
await settle(100);
// the down window answers immediately rather than waiting out timeoutMs
const down = await teacup.fetch(new Request('http://x/api/local/who'));
expect(down.status).toBe(503);
teapot = await startTeapot(port);
await settle(400);
const after = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'xyz' } }));
expect(after.status).toBe(200);
expect((await after.json()).auth).toEqual({ token: 'xyz' });
} finally {
await teacup.close();
await teapot.stop();
}
});
// CARD-19 (#124, D15): remote routes follow teapot membership on Bun too.
@Route('/remote')
class PingCtl {
@Get('/ping', { export: true })
ping() {
return { from: 'teapot' };
}
}
@Module({ mountpoint: '/api', controllers: [PingCtl] })
class PingTeapotModule {}
@Module({ mountpoint: '/api', controllers: [] })
class BareTeacupModule {}
async function startPingTeapot(port: number) {
const app = createApp({ modules: [PingTeapotModule], experimental: true, mesh: { secret: SECRET } });
const server = await serveBun(app, { port });
return {
stop: async () => {
server.stop(true);
await app.close();
},
};
}
/** Polls `GET path` until it answers `status`, or fails after `ms`. */
async function untilStatus(app: ReturnType<typeof createApp>, path: string, status: number, ms = 3000) {
const deadline = Date.now() + ms;
let last = 0;
while (Date.now() < deadline) {
const res = await app.fetch(new Request(`http://x${path}`));
await res.text();
last = res.status;
if (last === status) return;
await settle(25);
}
throw new Error(`${path} answered ${last}, expected ${status}`);
}
const pingTeacup = (port: number) =>
createApp({
modules: [BareTeacupModule],
experimental: true,
mesh: {
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: SECRET }],
reconnect: { initialDelayMs: 20, maxDelayMs: 60 },
bootTimeoutMs: 100,
heartbeatMs: 50,
timeoutMs: 2000,
},
});
test('mesh on Bun: a teapot absent at boot has its routes served once it connects (CARD-19 T1)', async () => {
const port = freePort();
const teacup = pingTeacup(port);
let teapot: Awaited<ReturnType<typeof startPingTeapot>> | undefined;
try {
expect((await teacup.fetch(new Request('http://x/api/remote/ping'))).status).toBe(404);
teapot = await startPingTeapot(port);
await untilStatus(teacup, '/api/remote/ping', 200);
} finally {
await teacup.close();
await teapot?.stop();
}
});
test('mesh on Bun: a reconnecting teapot keeps its routes with 503 + retry-after (CARD-19 T2)', async () => {
const port = freePort();
const teapot = await startPingTeapot(port);
const teacup = pingTeacup(port);
try {
await untilStatus(teacup, '/api/remote/ping', 200);
await teapot.stop();
await untilStatus(teacup, '/api/remote/ping', 503);
const down = await teacup.fetch(new Request('http://x/api/remote/ping'));
expect(down.status).toBe(503);
expect(Number(down.headers.get('retry-after'))).toBeGreaterThanOrEqual(1);
const options = await teacup.fetch(new Request('http://x/api/remote/ping', { method: 'OPTIONS' }));
expect(options.headers.get('allow')).toBe('GET, HEAD, OPTIONS');
} finally {
await teacup.close();
}
});