test(runtimes): close the 1c exit-gate cells for health and compression on Deno and Bun #184

Merged
chrnx merged 1 commits from test/gate-1c-cells into develop 2026-10-30 15:55:02 +00:00
6 changed files with 221 additions and 5 deletions

No files matched your search

+11 -1
View File
@@ -4,7 +4,7 @@ import 'reflect-metadata';
import { test } from 'bun:test';
import { serveBun } from '../../src/bun.ts';
import { compressionApp } from '../fixtures/compression-app.ts';
import { checks, serverDoesNotCompress, ticksDuringGzip } from '../fixtures/compression-checks.ts';
import { checks, gzipOnlyRefusesBr, serverDoesNotCompress, ticksDuringGzip } from '../fixtures/compression-checks.ts';
for (const [name, run] of Object.entries(checks)) {
test(`compression on Bun.serve: ${name}`, async () => {
@@ -18,6 +18,16 @@ for (const [name, run] of Object.entries(checks)) {
});
}
test("compression on Bun.serve: T6 encodings ['gzip'] and a br-only client → identity", async () => {
const server = await serveBun(compressionApp({ compression: { encodings: ['gzip'] } }), { hostname: '127.0.0.1', port: 0 });
try {
await gzipOnlyRefusesBr(server.port);
} finally {
await server.close({ timeoutMs: 1000 });
}
});
// The documented Bun facts (CARD-16; `guides/runtimes.md`).
test('documented: Bun.serve does not compress by itself (core compression off → identity)', async () => {
const server = await serveBun(compressionApp(), { hostname: '127.0.0.1', port: 0 });
+89
View File
@@ -0,0 +1,89 @@
// CARD-17 T1–T3 on Bun: the probe states a load balancer reads. T1 goes through `app.fetch`, because
// `serveBun` boots before it binds and so never answers while booting; T2 and T3 go through a real
// `serveBun` listener. T5 (the drain window) lives in `shutdown.test.ts`.
//
// Run with: npm run test:bun (or: bun test test/bun/)
import 'reflect-metadata';
import { test, expect } from 'bun:test';
import { createApp, Route, Get, Module, Provider } from '../../src/index.ts';
import { serveBun } from '../../src/bun.ts';
@Route('/')
class Ctl {
@Get('/ping')
ping() {
return { ok: true };
}
}
const quiet = { debug() {}, info() {}, warn() {}, error() {} };
async function probe(res: Response) {
return { status: res.status, body: await res.json() };
}
test('CARD-17 T1: /__ready__ 503 booting while a slow provider boots, /__live__ 200', async () => {
let finish: () => void = () => {};
@Provider({ provides: 'slowDb' })
class SlowDb {
async provide() {
await new Promise<void>((resolve) => (finish = resolve));
return {};
}
}
@Module({ mountpoint: '/', controllers: [Ctl], providers: [SlowDb] })
class Slow {}
const app = createApp({ modules: [Slow], health: true, logger: quiet });
const booting = app.boot();
await Bun.sleep(10);
expect(await probe(await app.fetch(new Request('http://x/__ready__')))).toEqual({
status: 503,
body: { status: 'booting' },
});
expect(await probe(await app.fetch(new Request('http://x/__live__')))).toEqual({
status: 200,
body: { status: 'ok' },
});
finish();
await booting;
expect(await probe(await app.fetch(new Request('http://x/__ready__')))).toEqual({
status: 200,
body: { status: 'ok' },
});
});
test('CARD-17 T2: serveBun, booted with nothing degraded → 200 ok', async () => {
@Module({ mountpoint: '/', controllers: [Ctl] })
class M {}
const server = await serveBun(createApp({ modules: [M], health: true, logger: quiet }), { port: 0 });
try {
const base = `http://127.0.0.1:${server.port}`;
expect(await probe(await fetch(`${base}/__ready__`))).toEqual({ status: 200, body: { status: 'ok' } });
expect(await probe(await fetch(`${base}/__live__`))).toEqual({ status: 200, body: { status: 'ok' } });
} finally {
await server.close();
}
});
test('CARD-17 T3: serveBun, an optional provider failed → 200 degraded with its name', async () => {
@Provider({ provides: 'x', optional: true })
class X {
provide(): never {
throw new Error('down');
}
}
@Module({ mountpoint: '/', controllers: [Ctl], providers: [X] })
class D {}
const server = await serveBun(createApp({ modules: [D], health: true, logger: quiet }), { port: 0 });
try {
expect(await probe(await fetch(`http://127.0.0.1:${server.port}/__ready__`))).toEqual({
status: 200,
body: { status: 'degraded', degraded: ['x'] },
});
} finally {
await server.close();
}
});
+15 -1
View File
@@ -5,7 +5,7 @@
import 'npm:reflect-metadata';
import { serveDeno } from '../../src/deno.ts';
import { compressionApp } from '../fixtures/compression-app.ts';
import { checks, serverDoesNotCompress, ticksDuringGzip } from '../fixtures/compression-checks.ts';
import { checks, gzipOnlyRefusesBr, serverDoesNotCompress, ticksDuringGzip } from '../fixtures/compression-checks.ts';
for (const [name, run] of Object.entries(checks)) {
Deno.test(`compression on Deno.serve: ${name}`, async () => {
@@ -23,6 +23,20 @@ for (const [name, run] of Object.entries(checks)) {
});
}
Deno.test("compression on Deno.serve: T6 encodings ['gzip'] and a br-only client → identity", async () => {
const server = await serveDeno(compressionApp({ compression: { encodings: ['gzip'] } }), {
hostname: '127.0.0.1',
port: 0,
onListen: () => {},
});
try {
await gzipOnlyRefusesBr((server as unknown as { addr: { port: number } }).addr.port);
} finally {
await server.close({ timeoutMs: 1000 });
}
});
// The documented Deno facts (CARD-16; `guides/runtimes.md`). If one of these starts failing, Deno
// changed and the docs have to change with it — not the assertion.
Deno.test('documented: Deno.serve does not compress by itself (core compression off → identity)', async () => {
+86
View File
@@ -0,0 +1,86 @@
// CARD-17 T1–T3 on Deno: the probe states a load balancer reads. T1 goes through `app.fetch`, because
// `serveDeno` boots before it binds and so never answers while booting; T2 and T3 go through a real
// `serveDeno` listener. T5 (the drain window) lives in `shutdown.test.ts`.
//
// Run with: npm run test:deno
import 'reflect-metadata';
import { createApp, Route, Get, Module, Provider } from '../../src/index.ts';
import { serveDeno } from '../../src/deno.ts';
@Route('/')
class Ctl {
@Get('/ping')
ping() {
return { ok: true };
}
}
const quiet = { debug() {}, info() {}, warn() {}, error() {} };
async function expectProbe(res: Response, status: number, body: unknown): Promise<void> {
const got = await res.json();
if (res.status !== status || JSON.stringify(got) !== JSON.stringify(body)) {
throw new Error(`expected ${status} ${JSON.stringify(body)}, got ${res.status} ${JSON.stringify(got)}`);
}
}
Deno.test('CARD-17 T1: /__ready__ 503 booting while a slow provider boots, /__live__ 200', async () => {
let finish: () => void = () => {};
@Provider({ provides: 'slowDb' })
class SlowDb {
async provide() {
await new Promise<void>((resolve) => (finish = resolve));
return {};
}
}
@Module({ mountpoint: '/', controllers: [Ctl], providers: [SlowDb] })
class Slow {}
const app = createApp({ modules: [Slow], health: true, logger: quiet });
const booting = app.boot();
await new Promise((r) => setTimeout(r, 10));
await expectProbe(await app.fetch(new Request('http://x/__ready__')), 503, { status: 'booting' });
await expectProbe(await app.fetch(new Request('http://x/__live__')), 200, { status: 'ok' });
finish();
await booting;
await expectProbe(await app.fetch(new Request('http://x/__ready__')), 200, { status: 'ok' });
});
Deno.test('CARD-17 T2: serveDeno, booted with nothing degraded → 200 ok', async () => {
@Module({ mountpoint: '/', controllers: [Ctl] })
class M {}
const server = await serveDeno(createApp({ modules: [M], health: true, logger: quiet }), {
port: 0,
onListen: () => {},
});
try {
const base = `http://127.0.0.1:${(server as unknown as { addr: { port: number } }).addr.port}`;
await expectProbe(await fetch(`${base}/__ready__`), 200, { status: 'ok' });
await expectProbe(await fetch(`${base}/__live__`), 200, { status: 'ok' });
} finally {
await server.close();
}
});
Deno.test('CARD-17 T3: serveDeno, an optional provider failed → 200 degraded with its name', async () => {
@Provider({ provides: 'x', optional: true })
class X {
provide(): never {
throw new Error('down');
}
}
@Module({ mountpoint: '/', controllers: [Ctl], providers: [X] })
class D {}
const server = await serveDeno(createApp({ modules: [D], health: true, logger: quiet }), {
port: 0,
onListen: () => {},
});
try {
const base = `http://127.0.0.1:${(server as unknown as { addr: { port: number } }).addr.port}`;
await expectProbe(await fetch(`${base}/__ready__`), 200, { status: 'degraded', degraded: ['x'] });
} finally {
await server.close();
}
});
+1 -1
View File
@@ -45,7 +45,7 @@ export const HTML_BODY: string = (() => {
export const SMALL_BODY = JSON.stringify({ pad: 'x'.repeat(500) }); // 512 B, under the 1024 threshold
export const BIG_BODY = HTML_BODY.repeat(2); // 102 KiB
const PNG = 'P'.repeat(4096);
const TEXT = 'y'.repeat(4096);
export const TEXT = 'y'.repeat(4096);
const raw = (headers: Record<string, string>, status = 200) => (body: unknown) => ({
status,
+19 -2
View File
@@ -4,7 +4,7 @@
// which decodes nothing, so a double encoding cannot hide.
import http from 'node:http';
import zlib from 'node:zlib';
import { JSON_BODY, HTML_BODY, SSE_EVENTS, SSE_GAP_MS } from './compression-app';
import { JSON_BODY, HTML_BODY, TEXT, SSE_EVENTS, SSE_GAP_MS } from './compression-app';
interface Raw {
status: number;
@@ -57,7 +57,7 @@ async function negotiation(port: number): Promise<void> {
}
}
/** T7, T8, T11, T15: skips, and the 304 shape. */
/** T7, T8, T11, T15: skips, a handler's own coding, a 204, and the 304 shape. */
async function skips(port: number): Promise<void> {
for (const [path, vary] of [
['/c/small', false],
@@ -71,6 +71,12 @@ async function skips(port: number): Promise<void> {
check(hasVary(r) === vary, `${path}: Vary ${String(r.headers.vary)}`);
}
// T8: a handler that set its own content-encoding is left byte-for-byte alone, and a 204 has nothing to encode.
const pre = await get(port, '/c/pre', { 'accept-encoding': 'gzip, br' });
check(pre.headers['content-encoding'] === 'gzip' && decode(pre.bytes, undefined) === TEXT, 'T8: handler coding touched');
const empty = await get(port, '/c/empty', { 'accept-encoding': 'gzip' });
check(empty.status === 204 && empty.headers['content-encoding'] === undefined && !hasVary(empty), 'T8: 204 shape');
const head = await get(port, '/c/json', { 'accept-encoding': 'gzip' }, 'HEAD');
check(head.headers['content-encoding'] === undefined && hasVary(head), 'HEAD: encoded or no Vary');
const nm = await get(port, '/c/304', { 'accept-encoding': 'gzip' });
@@ -132,6 +138,17 @@ async function sse(port: number): Promise<Record<string, number[]>> {
export const checks = { negotiation, skips, etag, ndjson, sse };
/**
* T6: with `encodings: ['gzip']`, a client that accepts only br gets identity. Run against
* `compressionApp({ compression: { encodings: ['gzip'] } })`.
*/
export async function gzipOnlyRefusesBr(port: number): Promise<void> {
const r = await get(port, '/c/json', { 'accept-encoding': 'br' });
check(r.headers['content-encoding'] === undefined, `T6: encoded as ${String(r.headers['content-encoding'])}`);
check(hasVary(r), 'T6: no Vary');
check(decode(r.bytes, undefined) === JSON_BODY, 'T6: body differs');
}
/**
* How many 1 ms timer ticks fire while `zlib.gzip` (the async API core uses) compresses 16 MB of
* random bytes — incompressible, so the work takes a few hundred ms on any machine (repetitive