Skip to content

Commit

Permalink
fix(server): fix setupExitSignals usage (#2181)
Browse files Browse the repository at this point in the history
* 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
knagaitsev authored and hiroppy committed Aug 8, 2019
1 parent 21e7646 commit bbe410e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
11 changes: 9 additions & 2 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ const getVersions = require('../lib/utils/getVersions');
const options = require('./options');

let server;

setupExitSignals(server);
const serverData = {
server: null,
};
// we must pass an object that contains the server object as a property so that
// we can update this server property later, and setupExitSignals will be able to
// recognize that the server has been instantiated, because we will set
// serverData.server to the new server object.
setupExitSignals(serverData);

// Prefer the local installation of webpack-dev-server
if (importLocal(__filename)) {
Expand Down Expand Up @@ -98,6 +104,7 @@ function startDevServer(config, options) {

try {
server = new Server(compiler, options, log);
serverData.server = server;
} catch (err) {
if (err.name === 'ValidationError') {
log.error(colors.error(options.stats.colors, err.message));
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/setupExitSignals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const signals = ['SIGINT', 'SIGTERM'];

function setupExitSignals(server) {
function setupExitSignals(serverData) {
signals.forEach((signal) => {
process.on(signal, () => {
if (server) {
server.close(() => {
if (serverData.server) {
serverData.server.close(() => {
// eslint-disable-next-line no-process-exit
process.exit();
});
Expand Down
57 changes: 57 additions & 0 deletions test/server/utils/setupExitSignals.test.js
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);
});
});
});

0 comments on commit bbe410e

Please sign in to comment.