-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest.js
96 lines (81 loc) · 2.22 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var path = require('path');
var cp = require('child_process');
var test = require('tape');
var treeKill = require('tree-kill');
var psTree = require('../');
var scripts = {
parent: path.join(__dirname, 'exec', 'parent.js'),
child: path.join(__dirname, 'exec', 'child.js')
};
test('Spawn a Parent process which has ten Child processes', function (t) {
t.timeoutAfter(10000);
var parent = cp.spawn('node', [scripts.parent]);
parent.stdout.on('data', function (data) {
psTree(parent.pid, function (error, children) {
if (error) {
t.error(error);
t.end();
return;
}
t.equal(children.length, 10, 'There should be 10 active child processes');
if (children.length !== 10) {
t.comment(parent.pid.toString());
t.comment(JSON.stringify(children, null, 2));
}
treeKill(parent.pid, function(error) {
if (error) {
t.error(error);
t.end();
return;
}
t.end();
});
});
});
});
test('Spawn a Child Process which has zero Child processes', function (t) {
t.timeoutAfter(10000);
var child = cp.spawn('node', [scripts.child]);
child.stdout.on('data', function (data) {
psTree(child.pid, function (error, children) {
if (error) {
t.error(error);
t.end();
return;
}
t.equal(children.length, 0, 'There should be no active child processes');
if (children.length !== 0) {
t.comment(child.pid.toString());
t.comment(JSON.stringify(children, null, 2));
}
treeKill(child.pid, function(error) {
if (error) {
t.error(error);
t.end();
return;
}
t.end();
});
});
});
});
test('Call psTree without supplying a Callback', function (t) {
var errmsg = 'Error: childrenOfPid(pid, callback) expects callback';
// Attempt to call psTree without a callback
try {
psTree(1234);
} catch (e) {
t.equal(e.toString(), errmsg);
}
t.end();
});
test('Directly Execute bin/ps-tree.js', function (t) {
var child = cp.exec('node ./bin/ps-tree.js', function (error, data) {
if (error !== null) {
t.error(err);
t.end();
return;
}
t.end();
});
});