UNPKG

3.8 kBJavaScriptView Raw
1/* global describe, it */
2
3var exec = require('child_process').exec,
4 expect = require('chai').expect,
5 assert = require('assert')
6
7require('chai').should()
8require('tap').mochaGlobals()
9
10describe('signal-exit', function () {
11
12 it('receives an exit event when a process exits normally', function (done) {
13 exec(process.execPath + ' ./test/fixtures/end-of-execution.js', function (err, stdout, stderr) {
14 expect(err).to.equal(null)
15 stdout.should.match(/reached end of execution, 0, null/)
16 done()
17 })
18 })
19
20 it('receives an exit event when a process is terminated with sigint', function (done) {
21 exec(process.execPath + ' ./test/fixtures/sigint.js', function (err, stdout, stderr) {
22 assert(err)
23 stdout.should.match(/exited with sigint, null, SIGINT/)
24 done()
25 })
26 })
27
28 it('receives an exit event when a process is terminated with sigterm', function (done) {
29 exec(process.execPath + ' ./test/fixtures/sigterm.js', function (err, stdout, stderr) {
30 assert(err)
31 stdout.should.match(/exited with sigterm, null, SIGTERM/)
32 done()
33 })
34 })
35
36 it('receives an exit event when process.exit() is called', function (done) {
37 exec(process.execPath + ' ./test/fixtures/exit.js', function (err, stdout, stderr) {
38 err.code.should.equal(32)
39 stdout.should.match(/exited with process\.exit\(\), 32, null/)
40 done()
41 })
42 })
43
44 it('does not exit if user handles signal', function (done) {
45 exec(process.execPath + ' ./test/fixtures/signal-listener.js', function (err, stdout, stderr) {
46 assert(err)
47 assert.equal(stdout, 'exited calledListener=4, code=null, signal="SIGHUP"\n')
48 done()
49 })
50 })
51
52 it('ensures that if alwaysLast=true, the handler is run last (signal)', function (done) {
53 exec(process.execPath + ' ./test/fixtures/signal-last.js', function (err, stdout, stderr) {
54 assert(err)
55 stdout.should.match(/first counter=1/)
56 stdout.should.match(/last counter=2/)
57 done()
58 })
59 })
60
61 it('ensures that if alwaysLast=true, the handler is run last (normal exit)', function (done) {
62 exec(process.execPath + ' ./test/fixtures/exit-last.js', function (err, stdout, stderr) {
63 assert.ifError(err)
64 stdout.should.match(/first counter=1/)
65 stdout.should.match(/last counter=2/)
66 done()
67 })
68 })
69
70 it('works when loaded multiple times', function (done) {
71 exec(process.execPath + ' ./test/fixtures/multiple-load.js', function (err, stdout, stderr) {
72 assert(err)
73 stdout.should.match(/first counter=1, code=null, signal="SIGHUP"/)
74 stdout.should.match(/first counter=2, code=null, signal="SIGHUP"/)
75 stdout.should.match(/last counter=3, code=null, signal="SIGHUP"/)
76 stdout.should.match(/last counter=4, code=null, signal="SIGHUP"/)
77 done()
78 })
79 })
80
81 // TODO: test on a few non-OSX machines.
82 it('removes handlers when fully unwrapped', function (done) {
83 exec(process.execPath + ' ./test/fixtures/unwrap.js', function (err, stdout, stderr) {
84 // on Travis CI no err.signal is populated but
85 // err.code is 129 (which I think tends to be SIGHUP).
86 var expectedCode = process.env.TRAVIS ? 129 : null
87
88 assert(err)
89 if (!process.env.TRAVIS) err.signal.should.equal('SIGHUP')
90 expect(err.code).to.equal(expectedCode)
91 done()
92 })
93 })
94
95 it('does not load() or unload() more than once', function (done) {
96 exec(process.execPath + ' ./test/fixtures/load-unload.js', function (err, stdout, stderr) {
97 assert.ifError(err)
98 done()
99 })
100 })
101
102 it('handles uncatchable signals with grace and poise', function (done) {
103 exec(process.execPath + ' ./test/fixtures/sigkill.js', function (err, stdout, stderr) {
104 assert.ifError(err)
105 done()
106 })
107 })
108})