-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.js
234 lines (220 loc) · 8.31 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var test = require('tape');
var chalk = require('chalk');
var red = chalk.red, green = chalk.green, cyan = chalk.cyan;
var psTree = require('ps-tree'); // see: https://git.io/jBHZ
var exec = require('child_process').exec;
var terminate = require('../terminate');
var terminate_promise = require('../promise');
var handlePsTreeCallback = require('../handlePsTreeCallback')
function assign(obj) {
for (var i = 1; i < arguments.length; i++) {
var vals = arguments[i]
if (vals) {
for (var key in vals) {
obj[key] = vals[key]
}
}
}
return obj
}
test(cyan('kills all processes'), function (t) {
t.plan(3);
var parent = exec("node " + require.resolve('./exec/parent.js'), function(error, stdout, stderr) {
if (error) {
// this happens even during successful testing because the parent gets SIGKILLed
console.log('exec error: ' + error);
}
})
setTimeout(function() {
console.log(cyan("Parent:" + parent.pid));
// list all the child processes
psTree(parent.pid, function (err, children) {
// console.log(red(err));
console.log(cyan("Children: "), children)
t.true(children.length > 0, green("✓ There are " + children.length + " active child processes"));
setTimeout(function () {
terminate(parent.pid, function (err) {
t.equal(err, null, green("✓ No Errors"))
})
setTimeout(function () {
psTree(parent.pid, function (err, children) {
// console.log("Children: ", children, '\n');
t.equal(children.length, 0, green("✓ No more active child processes (we killed them)"));
})
}, 200); // give psTree time to kill the processes
}, 10); // give the child process time to spawn
});
}, 200); // give the child process time to spawn
});
test(cyan('works with custom pollInterval'), function (t) {
t.plan(3);
var parent = exec("node " + require.resolve('./exec/parent.js'), function(error, stdout, stderr) {
if (error) {
console.log('exec error: ' + error);
}
})
setTimeout(function() {
console.log(cyan("Parent:"+parent.pid));
// list all the child processes
psTree(parent.pid, function(err, children) {
// console.log(red(err));
console.log(cyan("Children: "), children)
t.true(children.length > 0, green("✓ There are " + children.length + " active child processes"));
setTimeout(function () {
terminate(parent.pid, {pollInterval: 1000}, function (err) {
t.equal(err, null, green("✓ No Errors"))
})
setTimeout(function () {
psTree(parent.pid, function (err, children) {
// console.log("Children: ", children, '\n');
t.equal(children.length, 0, green("✓ No more active child processes (we killed them)"));
// t.end();
})
}, 200); // give psTree time to kill the processes
}, 10); // give the child process time to spawn
});
},200); // give the child process time to spawn
});
test(cyan('works when process takes awhile to exit'), function (t) {
t.plan(3);
var parent = exec(
"node " + require.resolve('./exec/parent.js'),
{env: assign({}, process.env, {KILL_DELAY: '1000'})},
function(error, stdout, stderr) {
if (error) {
console.log('exec error: ' + error);
}
}
)
setTimeout(function() {
console.log(cyan("Parent:"+parent.pid));
// list all the child processes
psTree(parent.pid, function(err, children) {
// console.log(red(err));
console.log(cyan("Children: "), children)
t.true(children.length > 0, green("✓ There are " + children.length + " active child processes"));
setTimeout(function () {
terminate(parent.pid, function (err) {
t.equal(err, null, green("✓ No Errors"))
})
setTimeout(function () {
psTree(parent.pid, function (err, children) {
// console.log("Children: ", children, '\n');
t.equal(children.length, 0, green("✓ No more active child processes (we killed them)"));
})
}, 1500); // give psTree time to kill the processes
}, 10); // give the child process time to spawn
});
},200); // give the child process time to spawn
});
if (process.platform !== 'win32') {
test(cyan('errors when process takes too long to exit'), function (t) {
var parent = exec(
"node " + require.resolve('./exec/parent.js'),
{env: assign({}, process.env, {KILL_DELAY: '2500'})},
function (error, stdout, stderr) {
if (error) {
console.log('exec error: ' + error);
}
}
)
setTimeout(function () {
console.log(cyan("Parent:" + parent.pid));
// list all the child processes
psTree(parent.pid, function (err, children) {
// console.log(red(err));
console.log(cyan("Children: "), children)
t.true(children.length > 0, green("✓ There are " + children.length + " active child processes"));
setTimeout(function () {
terminate(parent.pid, 'SIGINT', {timeout: 1000}, function (err) {
t.assert(err && /^timed out waiting for pids \d+(, \d+)* to exit$/.test(err.message), green("✓ got expected error message"));
t.end();
})
}, 10); // give the child process time to spawn
});
}, 200); // give the child process time to spawn
});
test(cyan('sends signal user passed'), function (t) {
t.plan(4);
var parent = exec("node " + require.resolve('./exec/parent.js'), function (error, stdout, stderr) {
t.assert(/^parent got SIGINT$/m.test(stdout), green("✓ parent reported getting SIGINT"));
if (error) {
console.log('exec error: ' + error);
}
})
setTimeout(function () {
console.log(cyan("Parent:" + parent.pid));
// list all the child processes
psTree(parent.pid, function (err, children) {
console.log(cyan("Children: "), children)
t.true(children.length > 0, green("✓ There are " + children.length + " active child processes"));
setTimeout(function () {
terminate(parent.pid, 'SIGINT', function (err) {
t.equal(err, null, green("✓ No Errors"))
})
setTimeout(function () {
psTree(parent.pid, function (err, children) {
// console.log("Children: ", children, '\n');
t.equal(children.length, 0, green("✓ No more active child processes (we killed them)"));
})
}, 200); // give psTree time to kill the processes
}, 10); // give the child process time to spawn
});
}, 200); // give the child process time to spawn
});
}
test(cyan('Attempt to terminate without providing a Parent Process ID'), function (t) {
var errmsg = "Error: No pid supplied to Terminate!"
try {
terminate(); // this should throw an error
}
catch (e) {
t.equal(e.toString(), errmsg, green("✓ Fails when no callback supplied (as expected)"))
t.end(); // nothing to test
}
});
test(cyan('Terminate a process without providing a callback'), function (t) {
t.plan(1);
var child = exec("node " + require.resolve('./exec/parent.js'), function(error, stdout, stderr) { });
setTimeout(function(){
terminate(child.pid);
setTimeout(function() {
psTree(child.pid, function (err, children) {
if(err){
console.log(err);
}
t.equal(children.length, 0, green("✓ No more active child processes"));
});
},1000); // give psTree time to kill the processes
},200); // give the child process time to spawn
});
test(cyan("errors if parent pid doesn't exist"), function (t) {
var pid = 9000;
function exists() {
try {
process.kill(pid, 0);
return true;
} catch (err) {
return false;
}
}
while (exists()) {
pid++;
}
terminate(pid, function(err) {
t.assert(err, green("✓ got error"));
t.end();
});
});
test(cyan('immediately passes along errors from psTree'), function (t) {
var error = new Error('test');
handlePsTreeCallback(error, undefined, 1, undefined, undefined, function (err) {
t.equal(error, err, green("✓ got expected error"));
t.end();
});
});
test(cyan('handles errors from psTree without callback'), function (t) {
var error = new Error('test');
handlePsTreeCallback(error, undefined, 1);
setTimeout(function () { t.end(); }, 500);
});