summaryrefslogtreecommitdiff
path: root/tst/signals.test.ts
blob: 14b4da6feb926e5dfee362de33314b07f7463906 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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));
    });
});