|
6 | 6 | // tslint:disable:no-any max-func-body-length no-invalid-this max-classes-per-file
|
7 | 7 |
|
8 | 8 | import { expect } from 'chai';
|
9 |
| -import { spawn } from 'child_process'; |
| 9 | +import { ChildProcess, spawn } from 'child_process'; |
10 | 10 | import { ProcessService } from '../../../client/common/process/proc';
|
11 |
| -import { createDeferred } from '../../../client/common/utils/async'; |
| 11 | +import { createDeferred, Deferred } from '../../../client/common/utils/async'; |
12 | 12 | import { PYTHON_PATH } from '../../common';
|
13 | 13 |
|
| 14 | +interface IProcData { |
| 15 | + proc: ChildProcess; |
| 16 | + exited: Deferred<Boolean>; |
| 17 | +} |
| 18 | + |
14 | 19 | suite('Process - Process Service', function () {
|
15 | 20 | // tslint:disable-next-line:no-invalid-this
|
16 | 21 | this.timeout(5000);
|
17 |
| - let procIdsToKill: number[] = []; |
| 22 | + const procsToKill: IProcData[] = []; |
18 | 23 | teardown(() => {
|
19 |
| - // tslint:disable-next-line:no-require-imports |
20 |
| - const killProcessTree = require('tree-kill'); |
21 |
| - procIdsToKill.forEach(pid => { |
22 |
| - try { |
23 |
| - killProcessTree(pid); |
24 |
| - } catch { |
25 |
| - // Ignore. |
| 24 | + procsToKill.forEach(p => { |
| 25 | + if (!p.exited.resolved) { |
| 26 | + p.proc.kill(); |
26 | 27 | }
|
27 | 28 | });
|
28 |
| - procIdsToKill = []; |
29 | 29 | });
|
30 | 30 |
|
31 |
| - function spawnProc() { |
| 31 | + function spawnProc(): IProcData { |
32 | 32 | const proc = spawn(PYTHON_PATH, ['-c', 'while(True): import time;time.sleep(0.5);print(1)']);
|
33 | 33 | const exited = createDeferred<Boolean>();
|
34 | 34 | proc.on('exit', () => exited.resolve(true));
|
35 |
| - procIdsToKill.push(proc.pid); |
| 35 | + procsToKill.push({ proc, exited }); |
36 | 36 |
|
37 |
| - return { pid: proc.pid, exited: exited.promise }; |
| 37 | + return procsToKill[procsToKill.length - 1]; |
38 | 38 | }
|
39 | 39 |
|
40 | 40 | test('Process is killed', async () => {
|
41 | 41 | const proc = spawnProc();
|
42 | 42 |
|
43 |
| - ProcessService.kill(proc.pid); |
| 43 | + ProcessService.kill(proc.proc.pid); |
44 | 44 |
|
45 |
| - expect(await proc.exited).to.equal(true, 'process did not die'); |
| 45 | + expect(await proc.exited.promise).to.equal(true, 'process did not die'); |
46 | 46 | });
|
47 | 47 | test('Process is alive', async () => {
|
48 | 48 | const proc = spawnProc();
|
49 | 49 |
|
50 |
| - expect(ProcessService.isAlive(proc.pid)).to.equal(true, 'process is not alive'); |
| 50 | + expect(ProcessService.isAlive(proc.proc.pid)).to.equal(true, 'process is not alive'); |
51 | 51 | });
|
52 | 52 |
|
53 | 53 | });
|
0 commit comments