Skip to content

Commit

Permalink
fix: fix process exit not working on Windows (#132)
Browse files Browse the repository at this point in the history
* feat: set NODE_ENV to development by default

* fix: fix process exit not working on windows

* test: add testcases to signal handle

* test: ignore win32 test
  • Loading branch information
xuchaoying authored Oct 24, 2019
1 parent a8fc920 commit 69ff38e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/svrx/__tests__/spec/svrx.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,36 @@ describe('Public API', () => {
});
});
});

describe('Signal handling', () => {
after(() => {
process.removeAllListeners('SIGTERM');
process.removeAllListeners('SIGINT');
});

describe('Signal handling', () => {
['SIGINT', 'SIGTERM'].forEach((SIGNAL) => {
describe(`${SIGNAL}`, () => {
let sandbox;
let exitStub;

beforeEach(() => {
sandbox = sinon.createSandbox({ useFakeTimers: true });
exitStub = sandbox.stub(process, 'exit');
});

afterEach(() => {
sandbox.restore();
});

it(`should call 'process.exit()' when receiving a ${SIGNAL}`, (done) => {
process.once(SIGNAL, () => {
sinon.assert.calledOnce(exitStub);
done();
});
process.kill(process.pid, SIGNAL);
});
});
});
});
});
23 changes: 23 additions & 0 deletions packages/svrx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ function create(options) {
return mapSvrxToExports(new Svrx(options));
}

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

/* istanbul ignore if */
if (process.platform === 'win32') {
const rl = require('readline').createInterface({ // eslint-disable-line
input: process.stdin,
output: process.stdout,
});
rl.on('SIGINT', () => {
process.emit('SIGINT');
});
rl.on('SIGTERM', () => {
process.emit('SIGTERM');
});
}

process.on('SIGINT', () => {
process.exit();
});
process.on('SIGTERM', () => {
process.exit();
});

module.exports = create;

create.create = create;

0 comments on commit 69ff38e

Please sign in to comment.