-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
test.js
67 lines (54 loc) · 1.47 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
import process from 'node:process';
import childProcess from 'node:child_process';
import test from 'ava';
import psList from './index.js';
const isWindows = process.platform === 'win32';
const nodeBinaryName = isWindows ? 'node.exe' : 'node';
const testBinaryName = isWindows ? nodeBinaryName : 'ava';
test('main', async t => {
const list = await psList();
if (isWindows) {
t.true(list.some(x => x.name.includes(testBinaryName)));
} else {
t.true(list.some(x => x.cmd.includes(testBinaryName)));
}
t.true(
list.every(x =>
typeof x.pid === 'number'
&& typeof x.name === 'string'
&& typeof x.ppid === 'number',
),
);
if (!isWindows) {
t.true(
list.every(x =>
typeof x.cmd === 'string'
&& typeof x.cpu === 'number'
&& typeof x.memory === 'number'
&& typeof x.uid === 'number',
),
);
}
});
test('custom binary', async t => {
const args = ['./fixtures/sleep-forever.js'];
for (let i = 0; i < 100; i++) {
args.push(`arg${i}`);
}
const sleepForever = childProcess.spawn(nodeBinaryName, args);
const list = await psList();
await new Promise(resolve => {
sleepForever.kill(9);
sleepForever.once('exit', () => {
resolve();
});
});
const record = list.find(process => process.pid === sleepForever.pid);
t.is(record.pid, sleepForever.pid);
t.is(record.name, nodeBinaryName);
t.is(record.ppid, process.pid);
if (!isWindows) {
t.is(record.cmd, `${nodeBinaryName} ${args.join(' ')}`);
t.is(record.uid, process.getuid());
}
});