|
1 | 1 | var path = require('path');
|
2 |
| -var test = require('tape'); |
3 |
| -var chalk = require('chalk'); |
4 | 2 | var cp = require('child_process');
|
| 3 | + |
| 4 | +var test = require('tape'); |
5 | 5 | var treeKill = require('tree-kill');
|
6 |
| -var psTree = require('../'); |
7 | 6 |
|
8 |
| -var red = chalk.red, |
9 |
| - green = chalk.green, |
10 |
| - cyan = chalk.cyan; |
| 7 | +var psTree = require('../'); |
11 | 8 |
|
12 | 9 | var scripts = {
|
13 | 10 | parent: path.join(__dirname, 'exec', 'parent.js'),
|
14 | 11 | child: path.join(__dirname, 'exec', 'child.js')
|
15 | 12 | };
|
16 | 13 |
|
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]); |
19 | 17 |
|
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 | + }); |
26 | 40 | });
|
| 41 | + }); |
| 42 | +}); |
27 | 43 |
|
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 | + } |
34 | 68 | t.end();
|
35 | 69 | });
|
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 | + }); |
39 | 72 | });
|
40 | 73 |
|
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 | + |
43 | 77 | // 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); |
47 | 82 | }
|
48 | 83 |
|
49 | 84 | t.end();
|
50 | 85 | });
|
51 | 86 |
|
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 | + }); |
70 | 96 | });
|
0 commit comments