From b3b645ca6f66569efae9a0da0d536cbd74aed567 Mon Sep 17 00:00:00 2001 From: Alka Trivedi Date: Wed, 11 Sep 2024 23:46:01 +0530 Subject: [PATCH] fix: emitting error event in fill --- .gitignore | 1 + src/session-pool.ts | 7 +------ test/session-pool.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d4f03a0df..14050d4e4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ system-test/*key.json .DS_Store package-lock.json __pycache__ +.vscode \ No newline at end of file diff --git a/src/session-pool.ts b/src/session-pool.ts index cfc2a4360..69b1472a3 100644 --- a/src/session-pool.ts +++ b/src/session-pool.ts @@ -815,12 +815,7 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface { if (needed <= 0) { return; } - - try { - await this._createSessions(needed); - } catch (e) { - this.emit('error', e); - } + await this._createSessions(needed); } /** diff --git a/test/session-pool.ts b/test/session-pool.ts index 43599585d..78f029fe5 100644 --- a/test/session-pool.ts +++ b/test/session-pool.ts @@ -515,6 +515,36 @@ describe('SessionPool', () => { process.listeners('unhandledRejection').push(originalRejection!); } }); + + it('should catch unhandled rejection errors', async () => { + const error = new Error('Unexpected error') as grpc.ServiceError; + + sandbox.restore(); + sandbox.stub(sessionPool, '_fill').callsFake(async () => { + throw error; + }); + + const originalRejection = process.listeners('unhandledRejection').pop(); + if (originalRejection) { + process.removeListener('unhandledRejection', originalRejection!); + } + + process.once('unhandledRejection', err => { + assert.ifError(err); + }); + + const errorListener = sandbox.stub(); + sessionPool.on('error', errorListener); + + await sessionPool.open(); + + assert.strictEqual(errorListener.calledOnce, true); + assert.strictEqual(errorListener.firstCall.args[0], error); + + if (originalRejection) { + process.listeners('unhandledRejection').push(originalRejection!); + } + }); }); describe('release', () => {