test(rate-limit): measure that close() lets the process exit
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 <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+69
-1
No files matched your search
@@ -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<void> {
|
||||||
|
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();
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import { strict as assert } from 'node:assert';
|
import { strict as assert } from 'node:assert';
|
||||||
|
import { spawn } from 'node:child_process';
|
||||||
import { test } from 'node:test';
|
import { test } from 'node:test';
|
||||||
|
|
||||||
import { createApp } from '@green-tea/core';
|
import { createApp } from '@green-tea/core';
|
||||||
import type { App } 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 { TooManyRequests } from '../src/errors.ts';
|
||||||
import { rateLimit } from '../src/index.ts';
|
import { rateLimit } from '../src/index.ts';
|
||||||
import type { RateLimiter } 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 }).name, 'rateLimit');
|
||||||
assert.equal(rateLimit({ rules: RULES, provides: 'adminLimit' }).name, 'adminLimit');
|
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');
|
||||||
|
});
|
||||||
Reference in new issue
Block a user