Debian Codegraph @

workspaces/arborist/test/signal-handling.js

raw
1 const t = require('tap')
2
3 const onExit = require('../lib/signal-handling.js')
4 t.equal(onExit.process, process, 'uses the real process by default')
5
6 // ok, from here on out, use our mock
7 const EE = require('node:events')
8 const proc = onExit.process = new class MockProcess extends EE {
9 constructor () {
10 super()
11 this.pid = process.pid
12 }
13
14 // yes, yes, the actual process.kill doesn't return a promise, but
15 // we need to know when this async action is done to know when to
16 // continue on to other parts of the test.
17 kill (pid, signal) {
18 if (pid !== this.pid) {
19 throw Object.assign(new Error('wrong pid sent to kill() method'), {
20 expect: this.pid,
21 actual: pid,
22 })
23 }
24 return new Promise(res => process.nextTick(() => {
25 this.emit(signal)
26 res()
27 }))
28 }
29 }()
30
31 t.test('load and unload', async t => {
32 t.plan(2)
33
34 const unload = onExit(({ signal }) => t.equal(signal, 'SIGINT'))
35 unload()
36
37 // should not trigger for these
38 await Promise.all([
39 proc.kill(process.pid, 'SIGINT'),
40 proc.kill(process.pid, 'SIGINT'),
41 proc.kill(process.pid, 'SIGINT'),
42 ])
43
44 t.equal(proc.listeners('beforeExit').length, 0, 'did not leave beforeExit')
45 t.equal(proc.listeners('SIGINT').length, 0, 'did not leave SIGINT')
46
47 // calling multiple times is safe
48 unload()
49 unload()
50 })
51
52 t.test('load only listens to one event', async t => {
53 t.plan(2)
54 onExit(({ signal }) => t.equal(signal, 'SIGINT'))
55 await proc.kill(process.pid, 'SIGINT')
56 // should not trigger our onExit handler, but WILL call this one,
57 // since the beforeExit handler was assigned
58 proc.once('SIGINT', () => t.pass('got follow-up signal'))
59 proc.emit('beforeExit')
60 // give it a moment for the synthetic kill timeout to fire
61 await new Promise(res => setTimeout(res))
62 })
63
64 t.test('only respond to first signal', async t => {
65 t.plan(5)
66 onExit(({ signal }) => t.equal(signal, 'SIGINT'))
67 await proc.kill(process.pid, 'SIGINT')
68 await proc.kill(process.pid, 'SIGHUP')
69 await proc.kill(process.pid, 'SIGTERM')
70 proc.emit('beforeExit')
71 t.equal(proc.listeners('beforeExit').length, 0, 'did not leave beforeExit')
72 t.equal(proc.listeners('SIGINT').length, 0, 'did not leave SIGINT')
73 t.equal(proc.listeners('SIGHUP').length, 0, 'did not leave SIGHUP')
74 t.equal(proc.listeners('SIGTERM').length, 0, 'did not leave SIGTERM')
75 })
76
77 t.test('can do multiple loads in parallel', t => {
78 t.plan(2)
79
80 t.test('running two handlers', async t => {
81 t.plan(3)
82 onExit(({ signal }) => t.equal(signal, 'SIGINT', '1'))
83 onExit(({ signal }) => t.equal(signal, 'SIGINT', '2'))
84 await proc.kill(process.pid, 'SIGINT')
85 // should not trigger our onExit handler, but WILL call this one,
86 // since the beforeExit handler was assigned
87 proc.once('SIGINT', () => t.pass('got follow-up signal'))
88 proc.emit('beforeExit')
89 // give it a moment for the synthetic kill timeout to fire
90 await new Promise(res => setTimeout(res))
91 })
92
93 t.test('verify cleanup after both are finished', t => {
94 t.plan(4)
95 t.equal(proc.listeners('beforeExit').length, 0, 'did not leave beforeExit')
96 t.equal(proc.listeners('SIGINT').length, 0, 'did not leave SIGINT')
97 t.equal(proc.listeners('SIGHUP').length, 0, 'did not leave SIGHUP')
98 t.equal(proc.listeners('SIGTERM').length, 0, 'did not leave SIGTERM')
99 })
100 })
101
102 t.test('no max listener warning', t => {
103 t.plan(1)
104 const { emitWarning } = process
105 Object.defineProperty(process, 'emitWarning', {
106 value: (...args) => {
107 emitWarning.call(process, ...args)
108 throw new Error('no warnings should be emitted')
109 },
110 configurable: true,
111 })
112 t.teardown(() => Object.defineProperty(process, 'emitWarning', {
113 value: emitWarning,
114 configurable: true,
115 }))
116
117 const unloads = []
118 for (let i = 0; i < 1000; i++) {
119 unloads.push(onExit(() => {}))
120 }
121 for (const unload of unloads) {
122 unload()
123 }
124
125 t.pass('if no throw, then it worked')
126 })