-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): fix setupExitSignals usage (#2181)
* fix(server): pass server data to exit signal setup as object * test(server): add exit signal tests * style(test): fix lint problem
- Loading branch information
1 parent
21e7646
commit bbe410e
Showing
3 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
const setupExitSignals = require('../../../lib/utils/setupExitSignals'); | ||
|
||
describe('setupExitSignals', () => { | ||
let server; | ||
let exitSpy; | ||
const signals = ['SIGINT', 'SIGTERM']; | ||
|
||
beforeAll(() => { | ||
exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {}); | ||
server = { | ||
close: jest.fn((callback) => { | ||
callback(); | ||
}), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
exitSpy.mockReset(); | ||
server.close.mockClear(); | ||
signals.forEach((signal) => { | ||
process.removeAllListeners(signal); | ||
}); | ||
}); | ||
|
||
signals.forEach((signal) => { | ||
it(`should exit process (${signal}, server never defined)`, (done) => { | ||
setupExitSignals({ | ||
server: null, | ||
}); | ||
process.emit(signal); | ||
setTimeout(() => { | ||
expect(exitSpy.mock.calls.length).toEqual(1); | ||
done(); | ||
}, 1000); | ||
}); | ||
|
||
it(`should close server, then exit process (${signal}, server defined before signal)`, (done) => { | ||
const serverData = { | ||
server: null, | ||
}; | ||
setupExitSignals(serverData); | ||
|
||
setTimeout(() => { | ||
serverData.server = server; | ||
process.emit(signal); | ||
}, 500); | ||
|
||
setTimeout(() => { | ||
expect(server.close.mock.calls.length).toEqual(1); | ||
expect(exitSpy.mock.calls.length).toEqual(1); | ||
done(); | ||
}, 1500); | ||
}); | ||
}); | ||
}); |