Skip to content

Commit 08eaa8f

Browse files
committed
test(node-runtime-worker-thread): Wait for the child process to exit before stopping the tests
1 parent b0f3c6f commit 08eaa8f

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

packages/node-runtime-worker-thread/src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/* istanbul ignore file */
22

3-
import { once } from 'events';
43
import { ChildProcess } from 'child_process';
54
import { MongoClientOptions } from '@mongosh/service-provider-core';
65
import { Runtime } from '@mongosh/browser-runtime-core';
76
import { EvaluationListener } from '@mongosh/shell-evaluator';
8-
import spawnChildFromSource from './spawn-child-from-source';
7+
import spawnChildFromSource, { kill } from './spawn-child-from-source';
98
import { Caller, createCaller, exposeAll, WithClose } from './rpc';
109
import type { WorkerRuntime as WorkerThreadWorkerRuntime } from './worker-runtime';
1110
import childProcessProxySrc from 'inline-entry-loader!./child-process-proxy';
@@ -107,12 +106,7 @@ class WorkerRuntime implements Runtime {
107106
async terminate() {
108107
await this.initWorkerPromise;
109108
this.childProcess.kill('SIGTERM');
110-
if (
111-
this.childProcess.exitCode === null &&
112-
this.childProcess.signalCode === null
113-
) {
114-
await once(this.childProcess, 'exit');
115-
}
109+
await kill(this.childProcess);
116110
}
117111
}
118112

packages/node-runtime-worker-thread/src/spawn-child-from-source.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import chaiAsPromised from 'chai-as-promised';
33
import childProcess from 'child_process';
44
import { once } from 'events';
55

6-
import spawnChildFromSource from './spawn-child-from-source';
6+
import spawnChildFromSource, { kill } from './spawn-child-from-source';
77

88
chai.use(chaiAsPromised);
99

1010
describe('spawnChildFromSource', () => {
1111
it('should resolve with a child process', async() => {
1212
const spawned = await spawnChildFromSource('');
1313
expect(spawned).to.be.instanceof((childProcess as any).ChildProcess);
14-
spawned.kill('SIGTERM');
14+
await kill(spawned);
1515
});
1616

1717
it('should spawn a process with an ipc channel open', async() => {
@@ -21,7 +21,7 @@ describe('spawnChildFromSource', () => {
2121
spawned.send('Hi!');
2222
const [message] = await once(spawned, 'message');
2323
expect(message).to.equal('Hi!');
24-
spawned.kill('SIGTERM');
24+
await kill(spawned);
2525
});
2626

2727
it('should fail if process exited before successfully starting', () => {

packages/node-runtime-worker-thread/src/spawn-child-from-source.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ import {
88
StdioNull,
99
StdioPipe
1010
} from 'child_process';
11+
import { once } from 'events';
12+
13+
export async function kill(childProcess: ChildProcess, code: NodeJS.Signals | number = 'SIGTERM') {
14+
childProcess.kill(code);
15+
if (
16+
childProcess.exitCode === null &&
17+
childProcess.signalCode === null
18+
) {
19+
await once(childProcess, 'exit');
20+
}
21+
}
1122

1223
export default function spawnChildFromSource(
1324
src: string,
@@ -16,7 +27,7 @@ export default function spawnChildFromSource(
1627
_stdout: StdioNull | StdioPipe = 'inherit',
1728
_stderr: StdioNull | StdioPipe = 'inherit',
1829
): Promise<ChildProcess> {
19-
return new Promise((resolve, reject) => {
30+
return new Promise(async(resolve, reject) => {
2031
const readyToken = Date.now().toString(32);
2132

2233
const childProcess = spawn(process.execPath, {
@@ -25,6 +36,7 @@ export default function spawnChildFromSource(
2536
});
2637

2738
if (!childProcess.stdin) {
39+
await kill(childProcess);
2840
return reject(
2941
new Error("Can't write src to the spawned process, missing stdin")
3042
);
@@ -59,11 +71,11 @@ export default function spawnChildFromSource(
5971

6072
timeoutId =
6173
timeoutMs !== undefined
62-
? setTimeout(() => {
74+
? setTimeout(async() => {
75+
await kill(childProcess);
6376
reject(
6477
new Error('Timed out while waiting for child process to start')
6578
);
66-
childProcess.kill('SIGTERM');
6779
}, timeoutMs)
6880
: null;
6981
});

0 commit comments

Comments
 (0)