|
| 1 | +var test = require('tape'); |
| 2 | +var chalk = require('chalk'); |
| 3 | +var red = chalk.red, green = chalk.green, cyan = chalk.cyan; |
| 4 | + |
| 5 | +var cp = require('child_process'), |
| 6 | + psTree = require('../') |
| 7 | + |
| 8 | +test(cyan('Spawn a Parent process which has a Two Child Processes'), function (t) { |
| 9 | + var parent = cp.exec("node ./test/exec/parent.js", function(error, stdout, stderr) { |
| 10 | + }) |
| 11 | + setTimeout(function(){ |
| 12 | + psTree(parent.pid, function (err, children) { |
| 13 | + if(err){ |
| 14 | + console.log(err); |
| 15 | + } |
| 16 | + console.log(red("Children: "), children, '\n'); |
| 17 | + t.true(children.length > 0, green("✓ There are "+children.length+" active child processes")); |
| 18 | + cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID }))) |
| 19 | + }) |
| 20 | + setTimeout(function(){ |
| 21 | + psTree(parent.pid, function (err, children) { |
| 22 | + if(err){ |
| 23 | + console.log(err); |
| 24 | + } |
| 25 | + // console.log("Children: ", children, '\n'); |
| 26 | + // console.log(' ') |
| 27 | + t.equal(children.length, 0, green("✓ No more active child processes (we killed them)")); |
| 28 | + t.end(); |
| 29 | + }) |
| 30 | + },1000); // give psTree time to kill the processes |
| 31 | + },200); // give the child process time to spawn |
| 32 | +}); |
| 33 | + |
| 34 | +test(cyan('FORCE ERROR by calling psTree without supplying a Callback'), function (t) { |
| 35 | + var errmsg = "Error: childrenOfPid(pid, callback) expects callback" |
| 36 | + try { |
| 37 | + psTree(1234); // attempt to call psTree without a callback |
| 38 | + } |
| 39 | + catch(e){ |
| 40 | + // console.log(red(e)); |
| 41 | + t.equal(e.toString(), errmsg, green("✓ Fails when no callback supplied (as expected)")) |
| 42 | + } |
| 43 | + t.end(); |
| 44 | +}); |
| 45 | + |
| 46 | + |
| 47 | +test(cyan('Spawn a Child Process and psTree with a String as pid'), function (t) { |
| 48 | + var child = cp.exec("node ./test/exec/child.js", function(error, stdout, stderr) { }); |
| 49 | + setTimeout(function(){ |
| 50 | + psTree(child.pid.toString(), function (err, children) { |
| 51 | + if(err){ |
| 52 | + console.log(err); |
| 53 | + } |
| 54 | + cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID }))) |
| 55 | + }) |
| 56 | + setTimeout(function() { |
| 57 | + psTree(child.pid.toString(), function (err, children) { |
| 58 | + if(err){ |
| 59 | + console.log(err); |
| 60 | + } |
| 61 | + t.equal(children.length, 0, green("✓ No more active child processes")); |
| 62 | + t.end(); |
| 63 | + }); |
| 64 | + },1000); // give psTree time to kill the processes |
| 65 | + },200); // give the child process time to spawn |
| 66 | +}); |
0 commit comments