summaryrefslogtreecommitdiff
path: root/tst/signals.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tst/signals.test.ts')
-rw-r--r--tst/signals.test.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/tst/signals.test.ts b/tst/signals.test.ts
new file mode 100644
index 0000000..14b4da6
--- /dev/null
+++ b/tst/signals.test.ts
@@ -0,0 +1,70 @@
+import { Either, Signals } from '../lib/index';
+import { CollectingTrace, TestTraceable } from './test_utils';
+
+describe('process/signals (Signals.awaitClose)', () => {
+ const originalOn = process.on;
+
+ afterEach(() => {
+ // restore in case a test fails mid-way
+ (process.on as any) = originalOn;
+ jest.restoreAllMocks();
+ });
+
+ test('resolves right on SIGINT with no error', async () => {
+ const handlers: Record<string, () => void> = {};
+ jest.spyOn(process, 'on').mockImplementation(((evt: string, cb: () => void) => {
+ handlers[evt] = cb;
+ return process;
+ }) as any);
+
+ const close = jest.fn((cb: (err: Error | undefined) => void) => cb(undefined));
+ const trace = new CollectingTrace<any>();
+ const t = TestTraceable.of({ close }, trace);
+
+ const p = Signals.awaitClose(t);
+ handlers.SIGINT();
+
+ const res = await p;
+ expect(res.left().present()).toBe(false);
+ expect(close).toHaveBeenCalledTimes(1);
+
+ const flattened = trace.events.flatMap((e) => e);
+ expect(flattened).toEqual(expect.arrayContaining(['closing', 'finished']));
+ });
+
+ test('resolves left on SIGTERM with error', async () => {
+ const handlers: Record<string, () => void> = {};
+ jest.spyOn(process, 'on').mockImplementation(((evt: string, cb: () => void) => {
+ handlers[evt] = cb;
+ return process;
+ }) as any);
+
+ const close = jest.fn((cb: (err: Error | undefined) => void) => cb(new Error('boom')));
+ const trace = new CollectingTrace<any>();
+ const t = TestTraceable.of({ close }, trace);
+
+ const p = Signals.awaitClose(t);
+ handlers.SIGTERM();
+
+ const res = await p;
+ expect(res.left().get().message).toBe('boom');
+ expect(close).toHaveBeenCalledTimes(1);
+ });
+
+ test('close callback is optional error', async () => {
+ const handlers: Record<string, () => void> = {};
+ jest.spyOn(process, 'on').mockImplementation(((evt: string, cb: () => void) => {
+ handlers[evt] = cb;
+ return process;
+ }) as any);
+
+ const close = jest.fn((cb: (err: Error | undefined) => void) => cb(undefined));
+ const t = TestTraceable.of({ close }, new CollectingTrace<any>());
+
+ const p = Signals.awaitClose(t);
+ handlers.SIGINT();
+
+ const res = await p;
+ expect(res).toEqual(Either.right(undefined));
+ });
+});