stock-bot/libs/core/shutdown/test/shutdown.test.ts

206 lines
6.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { Shutdown } from '../src/shutdown';
describe('Shutdown', () => {
let shutdown: Shutdown;
beforeEach(() => {
// Reset singleton instance and remove any prior signal listeners
Shutdown.reset();
shutdown = Shutdown.getInstance({ timeout: 1000, autoRegister: false });
});
afterEach(() => {
// Clean up listeners and singleton
Shutdown.reset();
});
describe('getInstance', () => {
it('should return singleton instance', () => {
const instance1 = Shutdown.getInstance();
const instance2 = Shutdown.getInstance();
expect(instance1).toBe(instance2);
});
it('should use provided config on first call', () => {
Shutdown.reset();
const instance = Shutdown.getInstance({ timeout: 5000, autoRegister: false });
expect(instance).toBeDefined();
});
});
describe('handler registration', () => {
it('should register high priority handler', () => {
const handler = mock(async () => {});
shutdown.onShutdown(handler, 10, 'High Priority Task');
expect(shutdown['callbacks']).toHaveLength(1);
expect(shutdown['callbacks'][0].name).toBe('High Priority Task');
expect(shutdown['callbacks'][0].priority).toBe(10);
});
it('should register medium priority handler', () => {
const handler = mock(async () => {});
shutdown.onShutdown(handler, 50, 'Medium Priority Task');
expect(shutdown['callbacks']).toHaveLength(1);
expect(shutdown['callbacks'][0].priority).toBe(50);
});
it('should register low priority handler', () => {
const handler = mock(async () => {});
shutdown.onShutdown(handler, 90, 'Low Priority Task');
expect(shutdown['callbacks']).toHaveLength(1);
expect(shutdown['callbacks'][0].priority).toBe(90);
});
it('should register multiple handlers in order', () => {
const handler1 = mock(async () => {});
const handler2 = mock(async () => {});
const handler3 = mock(async () => {});
shutdown.onShutdown(handler1, 10, 'First');
shutdown.onShutdown(handler2, 10, 'Second');
shutdown.onShutdown(handler3, 10, 'Third');
expect(shutdown['callbacks']).toHaveLength(3);
expect(shutdown['callbacks'][0].name).toBe('First');
expect(shutdown['callbacks'][2].name).toBe('Third');
});
});
describe('shutdown process', () => {
it('should execute handlers in priority order', async () => {
const executionOrder: string[] = [];
const highHandler = mock(async () => {
executionOrder.push('high');
});
const mediumHandler = mock(async () => {
executionOrder.push('medium');
});
const lowHandler = mock(async () => {
executionOrder.push('low');
});
shutdown.onShutdown(lowHandler, 90, 'Low');
shutdown.onShutdown(mediumHandler, 50, 'Medium');
shutdown.onShutdown(highHandler, 10, 'High');
await shutdown.shutdown();
expect(executionOrder).toEqual(['high', 'medium', 'low']);
});
it('should only shutdown once', async () => {
const handler = mock(async () => {});
shutdown.onShutdown(handler, 10, 'Handler');
await shutdown.shutdown();
await shutdown.shutdown(); // Second call should be ignored
expect(handler).toHaveBeenCalledTimes(1);
});
it('should handle errors in handlers', async () => {
const errorHandler = mock(async () => {
throw new Error('Handler error');
});
const successHandler = mock(async () => {});
shutdown.onShutdown(errorHandler, 10, 'Error Handler');
shutdown.onShutdown(successHandler, 10, 'Success Handler');
await shutdown.shutdown();
expect(errorHandler).toHaveBeenCalled();
expect(successHandler).toHaveBeenCalled();
});
it('should respect timeout', async () => {
let releaseHandler!: () => void;
const slowHandler = mock(
() =>
new Promise<void>(resolve => {
releaseHandler = resolve;
})
);
let scheduledCallback: (() => void) | undefined;
let scheduledDelay: number | undefined;
const fakeTimer = {} as ReturnType<typeof setTimeout>;
const setTimeoutSpy = spyOn(globalThis, 'setTimeout').mockImplementation(((
cb: (...args: unknown[]) => void,
delay?: number
) => {
scheduledCallback = () => cb();
scheduledDelay = delay;
return fakeTimer;
}) as unknown as typeof setTimeout);
const clearTimeoutSpy = spyOn(globalThis, 'clearTimeout').mockImplementation(() => {});
try {
Shutdown.reset();
shutdown = Shutdown.getInstance({ timeout: 100, autoRegister: false });
shutdown.onShutdown(slowHandler, 10, 'Slow Handler');
const shutdownPromise = shutdown.shutdown();
expect(scheduledDelay).toBe(100);
expect(scheduledCallback).toBeDefined();
scheduledCallback!();
await expect(shutdownPromise).rejects.toThrow('Shutdown timeout');
expect(clearTimeoutSpy).toHaveBeenCalledWith(fakeTimer);
} finally {
releaseHandler?.();
setTimeoutSpy.mockRestore();
clearTimeoutSpy.mockRestore();
}
});
});
describe('reset', () => {
it('should clear all handlers', () => {
shutdown.onShutdown(async () => {}, 10, 'Handler 1');
shutdown.onShutdown(async () => {}, 50, 'Handler 2');
shutdown.onShutdown(async () => {}, 90, 'Handler 3');
// Manually clear callbacks to simulate reset
shutdown['callbacks'] = [];
expect(shutdown['callbacks']).toHaveLength(0);
});
it('should reset shutdown state', async () => {
const handler = mock(async () => {});
shutdown.onShutdown(handler, 10, 'Handler');
await shutdown.shutdown();
// Reset by creating new instance
Shutdown.reset();
shutdown = Shutdown.getInstance({ timeout: 1000, autoRegister: false });
shutdown.onShutdown(handler, 10, 'Handler');
await shutdown.shutdown();
expect(handler).toHaveBeenCalledTimes(2);
});
});
// Skip forceShutdown test as it's not implemented in current shutdown
// describe.skip('forceShutdown', () => {
// it('should exit process after timeout', async () => {
// // Skipped
// });
// });
});