136 lines
3.9 KiB
TypeScript
136 lines
3.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
|
import { Shutdown } from '../src/shutdown';
|
|
|
|
// These tests use the real `process` listener registry (no stubs) because the
|
|
// regression being guarded is listeners leaking across test files in one bun process.
|
|
const SIGNALS = ['SIGTERM', 'SIGINT'] as const;
|
|
|
|
function listenerCounts(): Record<(typeof SIGNALS)[number], number> {
|
|
return {
|
|
SIGTERM: process.listenerCount('SIGTERM'),
|
|
SIGINT: process.listenerCount('SIGINT'),
|
|
};
|
|
}
|
|
|
|
describe('Shutdown signal listener cleanup', () => {
|
|
const created: Shutdown[] = [];
|
|
let baseline: ReturnType<typeof listenerCounts>;
|
|
|
|
function track(instance: Shutdown): Shutdown {
|
|
created.push(instance);
|
|
return instance;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
Shutdown.reset();
|
|
baseline = listenerCounts();
|
|
});
|
|
|
|
afterEach(() => {
|
|
for (const instance of created.splice(0)) {
|
|
instance.dispose();
|
|
}
|
|
Shutdown.reset();
|
|
});
|
|
|
|
it('autoRegister adds exactly one SIGTERM and one SIGINT listener and dispose removes them', () => {
|
|
const shutdown = track(new Shutdown());
|
|
|
|
expect(listenerCounts()).toEqual({
|
|
SIGTERM: baseline.SIGTERM + 1,
|
|
SIGINT: baseline.SIGINT + 1,
|
|
});
|
|
|
|
shutdown.dispose();
|
|
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
});
|
|
|
|
it('autoRegister: false adds no process listeners', () => {
|
|
track(new Shutdown({ autoRegister: false }));
|
|
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
});
|
|
|
|
it('removeSignalHandlers leaves listeners owned by other code in place', () => {
|
|
const foreign = () => {};
|
|
process.on('SIGTERM', foreign);
|
|
try {
|
|
const shutdown = track(new Shutdown());
|
|
shutdown.removeSignalHandlers();
|
|
|
|
expect(process.listeners('SIGTERM')).toContain(foreign);
|
|
expect(listenerCounts()).toEqual({ SIGTERM: baseline.SIGTERM + 1, SIGINT: baseline.SIGINT });
|
|
} finally {
|
|
process.off('SIGTERM', foreign);
|
|
}
|
|
});
|
|
|
|
it('dispose and reset are idempotent', () => {
|
|
const shutdown = track(new Shutdown());
|
|
|
|
shutdown.dispose();
|
|
shutdown.dispose();
|
|
Shutdown.reset();
|
|
Shutdown.reset();
|
|
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
});
|
|
|
|
it('repeatedly creating and disposing instances never accumulates listeners', () => {
|
|
for (let i = 0; i < 25; i++) {
|
|
new Shutdown().dispose();
|
|
}
|
|
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
});
|
|
|
|
it('Shutdown.reset removes singleton listeners and the next getInstance builds a fresh instance', () => {
|
|
const first = Shutdown.getInstance();
|
|
expect(listenerCounts().SIGTERM).toBe(baseline.SIGTERM + 1);
|
|
|
|
Shutdown.reset();
|
|
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
const second = Shutdown.getInstance({ autoRegister: false });
|
|
track(second);
|
|
expect(second).not.toBe(first);
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
});
|
|
|
|
it('disposing a non-singleton instance keeps the active singleton and its listeners', () => {
|
|
const singleton = Shutdown.getInstance();
|
|
const other = track(new Shutdown());
|
|
|
|
other.dispose();
|
|
|
|
expect(Shutdown.getInstance()).toBe(singleton);
|
|
expect(listenerCounts()).toEqual({
|
|
SIGTERM: baseline.SIGTERM + 1,
|
|
SIGINT: baseline.SIGINT + 1,
|
|
});
|
|
});
|
|
|
|
it('disposing the singleton directly clears it so getInstance returns a new one', () => {
|
|
const singleton = Shutdown.getInstance({ autoRegister: false });
|
|
|
|
singleton.dispose();
|
|
|
|
const next = Shutdown.getInstance({ autoRegister: false });
|
|
expect(next).not.toBe(singleton);
|
|
});
|
|
|
|
it('a disposed instance keeps its shutdown callbacks usable without process listeners', async () => {
|
|
const shutdown = track(new Shutdown({ timeout: 1000 }));
|
|
const calls: string[] = [];
|
|
shutdown.onShutdown(() => {
|
|
calls.push('ran');
|
|
}, 10, 'probe');
|
|
|
|
shutdown.dispose();
|
|
await shutdown.shutdown();
|
|
|
|
expect(calls).toEqual(['ran']);
|
|
expect(listenerCounts()).toEqual(baseline);
|
|
});
|
|
});
|