diff --git a/src/integration-tests/debugClient.ts b/src/integration-tests/debugClient.ts index a8e6f38b..acb327a9 100644 --- a/src/integration-tests/debugClient.ts +++ b/src/integration-tests/debugClient.ts @@ -233,11 +233,13 @@ export class CdtDebugClient extends DebugClient { } /* - * Returns a promise that will resolve if an output event with a specific category was received. + * Returns a promise that will resolve if an output event + * with a specific category and optional output message was received. * The promise will be rejected if a timeout occurs. */ public async waitForOutputEvent( category: string, + output?: string, timeout: number = this.defaultTimeout ): Promise { const isOutputEvent = ( @@ -249,26 +251,30 @@ export class CdtDebugClient extends DebugClient { ); }; - const timer = setTimeout(() => { - throw new Error( - `no output event with category '${category}' received after ${timeout} ms` - ); - }, timeout); - - for (;;) { - const event = await new Promise((resolve) => - this.once('output', (e) => resolve(e)) - ); - - if (!isOutputEvent(event)) { - continue; - } - - if (event.body.category === category) { - clearTimeout(timer); - return event; - } - } + return new Promise((resolve, reject) => { + const outputProcessor = (event: DebugProtocol.OutputEvent) => { + if (isOutputEvent(event) && event.body.category === category) { + if (output === undefined || output === event.body.output) { + clearTimeout(timer); + this.off('output', outputProcessor); + resolve(event); + } + } + }; + const timer = setTimeout(() => { + this.off('output', outputProcessor); + reject( + new Error( + `no output event with category '${category}' ${ + output === undefined + ? '' + : `and output message '${output}'` + } received after ${timeout} ms` + ) + ); + }, timeout); + this.on('output', outputProcessor); + }); } /**