Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/postcss-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,21 @@ const main = async function main(
fs.unlinkSync(socketPath); // eslint-disable-line no-sync
};

const termHandler = () => {
process.exit(0);
};

server.on('close', () => {
process.removeListener('exit', handler);
process.removeListener('SIGINT', handler);
process.removeListener('SIGTERM', handler);
process.removeListener('SIGINT', termHandler);
process.removeListener('SIGTERM', termHandler);
process.removeListener('SIGUSR2', termHandler);
});

process.on('exit', handler);
process.on('SIGINT', handler);
process.on('SIGTERM', handler);
process.on('SIGINT', termHandler);
process.on('SIGTERM', termHandler);
process.on('SIGUSR2', termHandler);

resolve();
});
Expand Down
44 changes: 41 additions & 3 deletions test/postcss-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ describe('postcss-server', () => {
});

describe('main(...testArgs)', () => {
let signintHandlers;
let exitHandlers, exitStub, signintHandlers, sigtermHandlers,
sigusr2Handlers;

beforeEach(() => { signintHandlers = process.listeners('SIGINT'); });
beforeEach(() => {
exitHandlers = process.listeners('exit');
signintHandlers = process.listeners('SIGINT');
sigtermHandlers = process.listeners('SIGTERM');
sigusr2Handlers = process.listeners('SIGUSR2');
exitStub = stub(process, 'exit');
});
afterEach(() => { process.exit.restore(); });

beforeEach(invokeMain);
afterEach(closeServer);
Expand All @@ -77,13 +85,43 @@ describe('postcss-server', () => {
expect(server.address()).to.eql(testSocket);
});

it('observes SIGINT to cleanup server socket', () => {
it('observes SIGINT to exit the process', () => {
const newHandlers = process.listeners('SIGINT')
.slice(signintHandlers.length);

expect(newHandlers.length).to.eql(1);
newHandlers[0]();

expect(exitStub).to.have.been.called;
});

it('observes SIGTERM exit the process', () => {
const newHandlers = process.listeners('SIGTERM')
.slice(sigtermHandlers.length);

expect(newHandlers.length).to.eql(1);
newHandlers[0]();

expect(exitStub).to.have.been.called;
});

it('observes SIGUSR2 exit the process', () => {
const newHandlers = process.listeners('SIGUSR2')
.slice(sigusr2Handlers.length);

expect(newHandlers.length).to.eql(1);
newHandlers[0]();

expect(exitStub).to.have.been.called;
});

it('uses the exit handler to clean up the server socket', () => {
const newHandlers = process.listeners('exit')
.slice(exitHandlers.length);

expect(newHandlers.length).to.eql(1);
newHandlers[0]();

expect(fs.existsSync(testSocket)).to.be.false;
});

Expand Down