TeardownRegistry.run set a `drained` flag and returned at once to any later caller. That kept teardown from running twice, but a second close() made while the first was still awaiting its callbacks resolved straight away. A signal handler and a test hook closing together could then move on with a database the first call had not closed yet. This reached every close path: app.close() with or without a listen()ed server, and serveDeno/serveBun, which hand over to app.close(). The registry now keeps the promise of the teardown it started and returns it to every caller, so the callbacks still run once and each caller resolves only after they finish, within its own runTeardown budget. The drain window and closeApp needed no change: the health drain already shares one window, and a second server.close() waits for the same 'close' event. close() after a failed listen() still has no server and only runs teardown. GitHub #45. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
104 lines
3.7 KiB
TypeScript
104 lines
3.7 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import { TeardownRegistry, type TeardownFn } from '../src/lifecycle';
|
|
import type { Logger } from '../src/logger';
|
|
|
|
const silentLogger = (): Logger => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() });
|
|
|
|
describe('TeardownRegistry', () => {
|
|
it('runs callbacks newest first, so a dependant tears down before its dependency', async () => {
|
|
const order: string[] = [];
|
|
const registry = new TeardownRegistry();
|
|
// Providers register as they boot, and they boot in topological order — so `db` registering
|
|
// before `cache` is what a `cache` needing `db` actually looks like here.
|
|
registry.add(() => void order.push('db'));
|
|
registry.add(() => void order.push('cache'));
|
|
|
|
await registry.run(silentLogger());
|
|
|
|
expect(order).toEqual(['cache', 'db']);
|
|
});
|
|
|
|
it('awaits each callback before starting the next', async () => {
|
|
const order: string[] = [];
|
|
const registry = new TeardownRegistry();
|
|
registry.add(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
order.push('slow');
|
|
});
|
|
registry.add(() => void order.push('fast'));
|
|
|
|
await registry.run(silentLogger());
|
|
|
|
// 'fast' is registered last so it runs first; 'slow' must still have finished by return.
|
|
expect(order).toEqual(['fast', 'slow']);
|
|
});
|
|
|
|
it('logs a throwing callback and still runs the rest', async () => {
|
|
const logger = silentLogger();
|
|
const order: string[] = [];
|
|
const registry = new TeardownRegistry();
|
|
registry.add(() => void order.push('first'));
|
|
registry.add(() => {
|
|
throw new Error('boom');
|
|
});
|
|
registry.add(() => void order.push('third'));
|
|
|
|
await expect(registry.run(logger)).resolves.toBeUndefined();
|
|
expect(order).toEqual(['third', 'first']);
|
|
expect(logger.warn).toHaveBeenCalledOnce();
|
|
expect(vi.mocked(logger.warn).mock.calls[0][0]).toMatch(/boom/);
|
|
});
|
|
|
|
it('treats a rejecting async callback the same as a throwing one', async () => {
|
|
const logger = silentLogger();
|
|
const registry = new TeardownRegistry();
|
|
registry.add(() => Promise.reject(new Error('async boom')));
|
|
registry.add(() => Promise.resolve());
|
|
|
|
await expect(registry.run(logger)).resolves.toBeUndefined();
|
|
expect(vi.mocked(logger.warn).mock.calls[0][0]).toMatch(/async boom/);
|
|
});
|
|
|
|
it('does not re-run callbacks on a second close()', async () => {
|
|
let runs = 0;
|
|
const registry = new TeardownRegistry();
|
|
registry.add(() => void runs++);
|
|
|
|
await registry.run(silentLogger());
|
|
await registry.run(silentLogger());
|
|
|
|
expect(runs).toBe(1);
|
|
});
|
|
|
|
it('makes a concurrent second close() wait for the teardown the first one started (GitHub #45)', async () => {
|
|
const order: string[] = [];
|
|
const registry = new TeardownRegistry();
|
|
registry.add(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
order.push('torn down');
|
|
});
|
|
|
|
const logger = silentLogger();
|
|
await Promise.all([
|
|
registry.run(logger).then(() => order.push('first returned')),
|
|
registry.run(logger).then(() => order.push('second returned')),
|
|
]);
|
|
|
|
expect(order).toEqual(['torn down', 'first returned', 'second returned']);
|
|
});
|
|
|
|
it('resolves when nothing is registered, and reports an empty size', async () => {
|
|
const registry = new TeardownRegistry();
|
|
expect(registry.size).toBe(0);
|
|
await expect(registry.run(silentLogger())).resolves.toBeUndefined();
|
|
});
|
|
|
|
it('reports how many callbacks are registered', () => {
|
|
const registry = new TeardownRegistry();
|
|
const noop: TeardownFn = () => {};
|
|
registry.add(noop);
|
|
registry.add(noop);
|
|
expect(registry.size).toBe(2);
|
|
});
|
|
});
|