Skip to content

Commit

Permalink
fix: emitting error event in fill
Browse files Browse the repository at this point in the history
  • Loading branch information
alkatrivedi committed Sep 11, 2024
1 parent 992baaa commit b3b645c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
.vscode
7 changes: 1 addition & 6 deletions src/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b3b645c

Please sign in to comment.