| 1 | const tap = require('tap');
|
|---|
| 2 | const test = tap.test;
|
|---|
| 3 | const readFile = require('fs').readFileSync;
|
|---|
| 4 | const spawn = require('child_process').spawn;
|
|---|
| 5 | const pstree = require('../');
|
|---|
| 6 | const { tree, pidsForTree, getStat } = require('../lib/utils');
|
|---|
| 7 |
|
|---|
| 8 | if (process.platform !== 'darwin') {
|
|---|
| 9 | test('reads from /proc', async (t) => {
|
|---|
| 10 | const ps = await getStat();
|
|---|
| 11 | t.ok(ps.split('\n').length > 1);
|
|---|
| 12 | });
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | test('tree for live env', async (t) => {
|
|---|
| 16 | const pid = 4079;
|
|---|
| 17 | const fixture = readFile(__dirname + '/fixtures/out2', 'utf8');
|
|---|
| 18 | const ps = await tree(fixture);
|
|---|
| 19 | t.deepEqual(
|
|---|
| 20 | pidsForTree(ps, pid).map((_) => _.PID),
|
|---|
| 21 | ['4080']
|
|---|
| 22 | );
|
|---|
| 23 | });
|
|---|
| 24 |
|
|---|
| 25 | function testTree(t, runCallCount) {
|
|---|
| 26 | const sub = spawn('node', [`${__dirname}/fixtures/index.js`, runCallCount], {
|
|---|
| 27 | stdio: 'pipe',
|
|---|
| 28 | });
|
|---|
| 29 | setTimeout(() => {
|
|---|
| 30 | const pid = sub.pid;
|
|---|
| 31 |
|
|---|
| 32 | pstree(pid, (error, pids) => {
|
|---|
| 33 | pids.concat([pid]).forEach((p) => {
|
|---|
| 34 | spawn('kill', ['-s', 'SIGTERM', p]);
|
|---|
| 35 | });
|
|---|
| 36 |
|
|---|
| 37 | // the fixture launches `sh` which launches node which is why we
|
|---|
| 38 | // are looking for two processes.
|
|---|
| 39 | // Important: IDKW but MacOS seems to skip the `sh` process. no idea.
|
|---|
| 40 | t.equal(pids.length, runCallCount * 2);
|
|---|
| 41 | t.end();
|
|---|
| 42 | });
|
|---|
| 43 | }, 1000);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | test('can read full process tree', (t) => {
|
|---|
| 47 | testTree(t, 1);
|
|---|
| 48 | });
|
|---|
| 49 | test('can read full process tree with multiple processes', (t) => {
|
|---|
| 50 | testTree(t, 2);
|
|---|
| 51 | });
|
|---|