From f72c391646d6514f80775cc3c4e741deaab935e3 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 18 Sep 2026 13:25:17 -0500 Subject: [PATCH] test(rate-limit): measure that close() lets the process exit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rule 7 says nothing a plugin opens outlives close(), and the only portable probe is a process: a leaked interval keeps all three runtimes alive, a cleared one lets each exit in well under a second. Verified the test fails with onShutdown removed — it reports the process still alive after 2s — because a leak test that passes while leaking is worth nothing. Signed-off-by: Diego Resendez --- packages/rate-limit/test/_exit-probe.ts | 35 +++++++++++++++++++++ packages/rate-limit/test/rate-limit.test.ts | 35 ++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 packages/rate-limit/test/_exit-probe.ts diff --git a/packages/rate-limit/test/_exit-probe.ts b/packages/rate-limit/test/_exit-probe.ts new file mode 100644 index 0000000..7a55761 --- /dev/null +++ b/packages/rate-limit/test/_exit-probe.ts @@ -0,0 +1,35 @@ +// Not a test — the child the exit test spawns. Named with a leading underscore so no runner picks it +// up: `node --test`, `deno test` and `bun test` all discover by filename, and this matches none. +// +// It boots an app that has taken a hit (so the sweep timer is armed), closes it, and prints one line. +// Nothing here calls process.exit: whether the process ends is the measurement. +import { createApp, Get, Module, Route, needs } from '@green-tea/core'; + +import { rateLimit } from '../src/index.ts'; +import type { RateLimiter } from '../src/index.ts'; + +@Route('/') +class Probe { + @Get('/probe') + async probe(@needs('rateLimit') limit: RateLimiter) { + await limit('signin'); + + return { ok: true }; + } +} + +@Module({ mountpoint: '/', controllers: [Probe] }) +class ProbeModule {} + +async function main(): Promise { + const app = createApp({ + modules: [ProbeModule], + plugins: [rateLimit({ rules: { signin: { windowMs: 60_000, max: 10, message: 'too many' } } })], + }); + + await app.fetch(new Request('http://probe.test/probe')); + await app.close(); + console.log('closed'); +} + +void main(); diff --git a/packages/rate-limit/test/rate-limit.test.ts b/packages/rate-limit/test/rate-limit.test.ts index 970dc30..344f9e4 100644 --- a/packages/rate-limit/test/rate-limit.test.ts +++ b/packages/rate-limit/test/rate-limit.test.ts @@ -1,10 +1,11 @@ import { strict as assert } from 'node:assert'; +import { spawn } from 'node:child_process'; import { test } from 'node:test'; import { createApp } from '@green-tea/core'; import type { App } from '@green-tea/core'; -import { probeModule } from '../../../test/helpers.ts'; +import { probeModule, runtimeCommand } from '../../../test/helpers.ts'; import { TooManyRequests } from '../src/errors.ts'; import { rateLimit } from '../src/index.ts'; import type { RateLimiter } from '../src/index.ts'; @@ -207,3 +208,35 @@ test('the plugin is named after what it provides', () => { assert.equal(rateLimit({ rules: RULES }).name, 'rateLimit'); assert.equal(rateLimit({ rules: RULES, provides: 'adminLimit' }).name, 'adminLimit'); }); + +/** Runs the probe and reports how it ended. `timeoutMs` elapsing means it never exited. */ +function runProbe(timeoutMs: number): Promise<{ exited: boolean; output: string }> { + const { command, args } = runtimeCommand('packages/rate-limit/test/_exit-probe.ts'); + + return new Promise((resolve) => { + const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'inherit'] }); + let output = ''; + + child.stdout.on('data', (chunk: Buffer) => (output += chunk.toString())); + + const timer = setTimeout(() => { + child.kill('SIGKILL'); + resolve({ exited: false, output }); + }, timeoutMs); + + child.on('exit', () => { + clearTimeout(timer); + resolve({ exited: true, output }); + }); + }); +} + +// Convention rule 7, and the only test in the repository that measures it rather than asserting it. +// A leaked setInterval keeps Node, Deno and Bun alive indefinitely; a cleared one lets each exit in +// well under a second. The process is the probe, which is what makes this portable. +test('the app exits on its own after close(), because onShutdown cleared the sweep', async () => { + const { exited, output } = await runProbe(2_000); + + assert.ok(output.includes('closed'), `the probe never reached close(): ${output}`); + assert.ok(exited, 'the process was still alive 2s after close() — a timer outlived onShutdown'); +});