diff --git a/lib/ts/package.json b/lib/ts/package.json index 6ae2d3d..55bbd13 100644 --- a/lib/ts/package.json +++ b/lib/ts/package.json @@ -15,7 +15,7 @@ }, "scripts": { "build": "tsc", - "test": "vitest run --teardown-timeout=20000 --test-timeout=20000" + "test": "vitest run --teardown-timeout=20000 --test-timeout=20000 --sequence.concurrent" }, "dependencies": { "@types/node": "^22.10.5" diff --git a/lib/ts/src/lib.test.ts b/lib/ts/src/lib.test.ts index 4e4bae1..47ebca0 100644 --- a/lib/ts/src/lib.test.ts +++ b/lib/ts/src/lib.test.ts @@ -6,3 +6,9 @@ test('test connection timeout', async ({expect}) => { await parentIpc.start(); await expect(parentIpc.wait()).rejects.toThrowError('timed out'); }); + +test('test process stop before connection accept', async ({expect}) => { + const parentIpc = new ParentIPC('testdata/sleep3.sh', []); + await parentIpc.start(); + await expect(parentIpc.wait()).rejects.toThrowError('command exited before connection established'); +}); diff --git a/lib/ts/src/lib.ts b/lib/ts/src/lib.ts index 6bc4157..0b01179 100644 --- a/lib/ts/src/lib.ts +++ b/lib/ts/src/lib.ts @@ -45,6 +45,7 @@ abstract class IPCCommon { protected pendingCalls: Record void> = {}; protected stopRequested: boolean = false; protected processingCalls: number = 0; + protected ready = false; protected errorQueue = new AsyncQueue(); protected onClose?: () => void; @@ -84,6 +85,8 @@ abstract class IPCCommon { this.raiseErr(new Error(`${ e }`)); } }); + + this.ready = true; } protected processMsg(msg: Message): void { @@ -266,11 +269,16 @@ export class ParentIPC extends IPCCommon { async wait(): Promise { return new Promise(async (resolve, reject) => { - if (!this.cmd) throw new Error('Command is not started yet'); + if (!this.cmd) { + reject('Command is not started yet'); + return; + } this.cmd.addListener('close', (code, signal) => { if (signal || code) { if (signal) reject(new Error(`Process exited with signal ${ signal }`)); else reject(new Error(`Process exited with code ${ code }`)); + } else if(!this.ready) { + reject('command exited before connection established'); } else { resolve(); } diff --git a/lib/ts/testdata/sleep15.sh b/lib/ts/testdata/sleep15.sh index 8f5eec9..76f2852 100755 --- a/lib/ts/testdata/sleep15.sh +++ b/lib/ts/testdata/sleep15.sh @@ -1,5 +1,3 @@ #!/usr/bin/env bash -echo sleeping... sleep 15s -echo slept diff --git a/lib/ts/testdata/sleep3.sh b/lib/ts/testdata/sleep3.sh new file mode 100755 index 0000000..426a98a --- /dev/null +++ b/lib/ts/testdata/sleep3.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +sleep 3s