From 927d20945d664c55209fd95b05b2c68904f51acc Mon Sep 17 00:00:00 2001 From: Michael Booth Date: Thu, 13 Jul 2023 09:22:25 +0100 Subject: [PATCH] fix: Update events that are propagated from pool cluster to include remove (#2114) * Add test for events being propegated through promise wrapper for pool cluster * Propagate pool cluster remove event and don't propegate events that don't happen * Move pool cluster promise test to builtin-runner * Update PoolCluster.d.ts to match promise.d.ts for pool cluster events --- promise.d.ts | 2 +- promise.js | 2 +- .../pool-cluster/test-promise-wrapper.mjs | 33 +++++++++++++++++++ typings/mysql/lib/PoolCluster.d.ts | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/builtin-runner/integration/pool-cluster/test-promise-wrapper.mjs diff --git a/promise.d.ts b/promise.d.ts index f0c56b7e27..efe50005a6 100644 --- a/promise.d.ts +++ b/promise.d.ts @@ -117,7 +117,7 @@ export interface PoolCluster extends EventEmitter { on(event: string, listener: (args: any[]) => void): this; on(event: 'remove', listener: (nodeId: number) => void): this; - on(event: 'connection', listener: (connection: PoolConnection) => void): this; + on(event: 'warn', listener: (err: Error) => void): this; } export function createConnection(connectionUri: string): Promise; diff --git a/promise.js b/promise.js index c4bb609038..95c5808b45 100644 --- a/promise.js +++ b/promise.js @@ -448,7 +448,7 @@ class PromisePoolCluster extends EventEmitter { super(); this.poolCluster = poolCluster; this.Promise = thePromise || Promise; - inheritEvents(poolCluster, this, ['acquire', 'connection', 'enqueue', 'release']); + inheritEvents(poolCluster, this, ['warn', 'remove']); } getConnection() { diff --git a/test/builtin-runner/integration/pool-cluster/test-promise-wrapper.mjs b/test/builtin-runner/integration/pool-cluster/test-promise-wrapper.mjs new file mode 100644 index 0000000000..b66a661a27 --- /dev/null +++ b/test/builtin-runner/integration/pool-cluster/test-promise-wrapper.mjs @@ -0,0 +1,33 @@ +import { describe, it, before, after } from 'node:test'; +import assert from 'node:assert'; +import common from '../../../common.js'; +import { createPoolCluster } from "../../../../promise.js" + +describe('Test pool cluster', { timeout: 1000 }, () => { + + it('should propagate warn event to promise wrapper', (t, done) => { + + const poolCluster = createPoolCluster(); + /* eslint-disable no-invalid-this */ + poolCluster + .once('warn', function () { + assert.equal(this, poolCluster); + done(); + }) + /* eslint-enable no-invalid-this */ + poolCluster.poolCluster.emit('warn', new Error()); + }); + + it('should propagate remove event to promise wrapper', (t, done) => { + + const poolCluster = createPoolCluster(); + /* eslint-disable no-invalid-this */ + poolCluster + .once('remove', function () { + assert.equal(this, poolCluster); + done(); + }); + /* eslint-enable no-invalid-this */ + poolCluster.poolCluster.emit('remove'); + }); +}); diff --git a/typings/mysql/lib/PoolCluster.d.ts b/typings/mysql/lib/PoolCluster.d.ts index bf34111660..006efc35dc 100644 --- a/typings/mysql/lib/PoolCluster.d.ts +++ b/typings/mysql/lib/PoolCluster.d.ts @@ -80,7 +80,7 @@ declare class PoolCluster extends EventEmitter { on(event: string, listener: (args: any[]) => void): this; on(event: 'remove', listener: (nodeId: number) => void): this; - on(event: 'connection', listener: (connection: PoolConnection) => void): this; + on(event: 'warn', listener: (err: Error) => void): this; } export { PoolCluster };