Skip to content

Commit

Permalink
test: improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 12, 2017
1 parent 03dc352 commit 83e1788
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (options = {}) => {
const logger = options.logger || console;
const label = options.label || `graceful-process#${process.pid}`;
if (process[init]) {
logger.warn('[graceful-process] [%s] init already', label);
logger.warn('[%s] graceful-process init already', label);
return;
}
process[init] = true;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint *.js test",
"test": "npm run lint && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"cov": "cross-env COV=true egg-bin cov",
"autod": "autod",
"ci": "npm run lint && npm run cov"
},
Expand All @@ -25,6 +25,7 @@
"devDependencies": {
"autod": "^2.8.0",
"coffee": "^3.3.2",
"cross-env": "^5.0.1",
"egg-bin": "^3.4.2",
"egg-ci": "^1.7.0",
"eslint": "^3.19.0",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ http.createServer((req, res) => {
}).listen(8000);

console.log(`Worker ${process.pid} started`);
require('../..')({
logger: console,
label: 'test-child',
});
// run again should work
require('../..')();
6 changes: 4 additions & 2 deletions test/fixtures/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);

// Fork workers.
for (let i = 0; i < numCPUs; i++) {
for (let i = 0; i < 2; i++) {
cluster.fork();
}

Expand All @@ -30,6 +29,9 @@ if (cluster.isMaster) {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
if (req.url === '/suicide') {
cluster.worker.disconnect();
}
}).listen(8000);

console.log(`Worker ${process.pid} started`);
Expand Down
7 changes: 6 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const sleep = require('mz-modules/sleep');
const urllib = require('urllib');

const fixtures = path.join(__dirname, 'fixtures');
const waitStart = process.env.CI ? 20000 : 2000;
const waitStart = process.env.COV ? 30000 : 2000;

describe('test/index.test.js', () => {
describe('cluster', () => {
Expand All @@ -34,6 +34,11 @@ describe('test/index.test.js', () => {
const result = yield urllib.request('http://127.0.0.1:8000/');
assert(result.status === 200);
assert(result.data.toString() === 'hello world\n');
// suicide
const result2 = yield urllib.request('http://127.0.0.1:8000/suicide');
assert(result2.status === 200);
assert(result2.data.toString() === 'hello world\n');
yield sleep(1000);
// make sure all workers exit by itself after SIGTERM event fired
child.proc.kill('SIGTERM');
yield sleep(1000);
Expand Down

0 comments on commit 83e1788

Please sign in to comment.