Skip to content

Commit a91ed39

Browse files
simonepriindexzero
authored andcommitted
Make tests deterministic
1 parent 3329a8e commit a91ed39

File tree

5 files changed

+100
-103
lines changed

5 files changed

+100
-103
lines changed

package.json

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,17 @@
3333
"node": ">= 0.10"
3434
},
3535
"scripts": {
36-
"_comment": "https://github.com/gotwarlost/istanbul#usage-on-windows",
37-
"test": "istanbul cover node_modules/tape/bin/tape test/test.js test/direct.js",
38-
"coverage": "npm test && istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100",
39-
"codeclimate": "cross-env CODECLIMATE_REPO_TOKEN=84436b4f13c70ace9c62e7f04928bf23c234eb212c0232d39d7fb1535beb2da5 codeclimate < coverage/lcov.info"
36+
"test": "istanbul cover node_modules/tape/bin/tape test/test.js",
37+
"coverage": "cross-env CODECLIMATE_REPO_TOKEN=84436b4f13c70ace9c62e7f04928bf23c234eb212c0232d39d7fb1535beb2da5 node_modules/.bin/codeclimate-test-reporter < coverage/lcov.info"
4038
},
4139
"dependencies": {
4240
"event-stream": "=3.3.4"
4341
},
4442
"devDependencies": {
45-
"chalk": "^1.0.0",
46-
"codeclimate-test-reporter": "0.0.4",
47-
"cross-env": "^1.0.8",
48-
"istanbul": "^0.3.20",
49-
"pre-commit": "0.0.9",
50-
"precommit": "^1.1.5",
51-
"tape": "^3.0.3",
43+
"codeclimate-test-reporter": "^0.5.0",
44+
"cross-env": "^2.0.1",
45+
"istanbul": "^0.4.5",
46+
"tape": "^4.9.0",
5247
"tree-kill": "^1.1.0"
53-
},
54-
"pre-commit": [
55-
"coverage",
56-
"codeclimate"
57-
]
48+
}
5849
}

test/direct.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/exec/child.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// does nothing child process
2-
console.log("Child process.id: " + process.pid);
3-
console.log(" - - - - - - - - - - - - - - - - - - - - - - - ");
4-
5-
setTimeout(function () {
6-
/* Does nothing, but prevents exit */
7-
}, 1000);
1+
var started = false;
2+
setInterval(function() {
3+
if (started) return;
4+
console.log(process.pid);
5+
started = true;
6+
}, 100); // Does nothing, but prevents exit

test/exec/parent.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
var path = require('path');
12
var cp = require('child_process');
2-
var chalk = require('chalk');
3-
var red = chalk.red
4-
var green = chalk.green
5-
var cyan = chalk.cyan;
6-
var count = 0;
73

8-
while(count < 10) {
9-
var child = cp.exec("node ./test/exec/child.js", function(error, stdout, stderr) {
10-
console.log('stdout: ' + stdout);
11-
console.log(red('stderr: ' + stderr));
12-
if (error !== null) {
13-
console.log(red('exec error: ' + error));
14-
}
15-
})
4+
var started = false;
5+
var spawned = {};
166

17-
console.log("child pid: %s | count: %s", child.pid, ++count);
7+
for (var i = 0; i < 10; i++) {
8+
var child = cp.spawn('node', [path.join('test', 'exec', 'child.js')]);
9+
child.stdout.on('data', function (child) {
10+
spawned[child.pid] = true;
11+
}.bind(this, child));
1812
}
13+
14+
setInterval(function() {
15+
if (started) return;
16+
if (Object.keys(spawned).length !== 10) return;
17+
console.log(process.pid);
18+
started = true;
19+
}, 100); // Does nothing, but prevents exit

test/test.js

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,96 @@
11
var path = require('path');
2-
var test = require('tape');
3-
var chalk = require('chalk');
42
var cp = require('child_process');
3+
4+
var test = require('tape');
55
var treeKill = require('tree-kill');
6-
var psTree = require('../');
76

8-
var red = chalk.red,
9-
green = chalk.green,
10-
cyan = chalk.cyan;
7+
var psTree = require('../');
118

129
var scripts = {
1310
parent: path.join(__dirname, 'exec', 'parent.js'),
1411
child: path.join(__dirname, 'exec', 'child.js')
1512
};
1613

17-
test(cyan('Spawn a Parent process which has a Two Child Processes'), function (t) {
18-
var parent = cp.exec('node ' + scripts.parent, function (error, stdout, stderr) {});
14+
test('Spawn a Parent process which has ten Child processes', function (t) {
15+
t.timeoutAfter(10000);
16+
var parent = cp.spawn('node', [scripts.parent]);
1917

20-
setTimeout(function () {
21-
psTree(parent.pid, function (err, children) {
22-
if (err) { console.log(err); }
23-
console.log(red('Children: '), children, '\n');
24-
t.true(children.length > 0, green('✓ There are ' + children.length + ' active child processes'));
25-
treeKill(parent.pid);
18+
parent.stdout.on('data', function (data) {
19+
psTree(parent.pid, function (error, children) {
20+
if (error) {
21+
t.error(error);
22+
t.end();
23+
return;
24+
}
25+
26+
t.equal(children.length, 10, 'There should be 10 active child processes');
27+
if (children.length !== 10) {
28+
t.comment(parent.pid.toString());
29+
t.comment(JSON.stringify(children, null, 2));
30+
}
31+
32+
treeKill(parent.pid, function(error) {
33+
if (error) {
34+
t.error(error);
35+
t.end();
36+
return;
37+
}
38+
t.end();
39+
});
2640
});
41+
});
42+
});
2743

28-
setTimeout(function () {
29-
psTree(parent.pid, function (err, children) {
30-
if (err) { console.log(err); }
31-
// console.log('Children: ', children, '\n');
32-
// console.log(' ')
33-
t.equal(children.length, 0, green('✓ No more active child processes (we killed them)'));
44+
test('Spawn a Child Process which has zero Child processes', function (t) {
45+
t.timeoutAfter(10000);
46+
var child = cp.spawn('node', [scripts.child]);
47+
48+
child.stdout.on('data', function (data) {
49+
psTree(child.pid, function (error, children) {
50+
if (error) {
51+
t.error(error);
52+
t.end();
53+
return;
54+
}
55+
56+
t.equal(children.length, 0, 'There should be no active child processes');
57+
if (children.length !== 0) {
58+
t.comment(child.pid.toString());
59+
t.comment(JSON.stringify(children, null, 2));
60+
}
61+
62+
treeKill(child.pid, function(error) {
63+
if (error) {
64+
t.error(error);
65+
t.end();
66+
return;
67+
}
3468
t.end();
3569
});
36-
}, 2000); // give psTree time to kill the processes
37-
}, 500); // give the child process time to spawn
38-
// need more time on a slow(or heavy load server). maybe promise.then is better instead of the timeout
70+
});
71+
});
3972
});
4073

41-
test(cyan('FORCE ERROR by calling psTree without supplying a Callback'), function (t) {
42-
var errmsg = 'Error: childrenOfPid(pid, callback) expects callback'
74+
test('Call psTree without supplying a Callback', function (t) {
75+
var errmsg = 'Error: childrenOfPid(pid, callback) expects callback';
76+
4377
// Attempt to call psTree without a callback
44-
try { psTree(1234); }
45-
catch (e) {
46-
t.equal(e.toString(), errmsg, green('✓ Fails when no callback supplied (as expected)'))
78+
try {
79+
psTree(1234);
80+
} catch (e) {
81+
t.equal(e.toString(), errmsg);
4782
}
4883

4984
t.end();
5085
});
5186

52-
53-
test(cyan('Spawn a Child Process and psTree with a String as pid'), function (t) {
54-
var child = cp.exec('node ' + scripts.child, function(error, stdout, stderr) {});
55-
setTimeout(function(){
56-
psTree(child.pid.toString(), function (err, children) {
57-
if (err) { console.log(err); }
58-
// cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })))
59-
treeKill(child.pid);
60-
});
61-
62-
setTimeout(function() {
63-
psTree(child.pid.toString(), function (err, children) {
64-
if (err) { console.log(err); }
65-
t.equal(children.length, 0, green('✓ No more active child processes'));
66-
t.end();
67-
});
68-
}, 1000); // give psTree time to kill the processes
69-
}, 200); // give the child process time to spawn
87+
test('Directly Execute bin/ps-tree.js', function (t) {
88+
var child = cp.exec('node ./bin/ps-tree.js', function (error, data) {
89+
if (error !== null) {
90+
t.error(err);
91+
t.end();
92+
return;
93+
}
94+
t.end();
95+
});
7096
});

0 commit comments

Comments
 (0)