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();
|
||||
Reference in new issue
Block a user