From 16507a971bfc1d0d0e9d29a35f5253e1c1a94ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Tue, 12 Mar 2024 16:34:55 +0100 Subject: [PATCH] chore: re-add max listeners (#6522) ## About the changes Some tests are reporting this error: `MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 UPDATE_REVISION listeners added to [ConfigurationRevisionService]. Use emitter.setMaxListeners() to increase limit` I thought it's because of this change https://github.com/Unleash/unleash/pull/6400#discussion_r1511494201 but it was not. I've managed to get a trace of the issue following this https://stackoverflow.com/q/62897235/239613 and now we can identify `ConfigurationRevisionService` as one explanation. The reason is that it's a singleton that should be cleaned up after tests. E.g.: https://github.com/Unleash/unleash/actions/runs/8248332132/job/22558419656?pr=6517#step:8:15 --- package.json | 2 +- .../features/feature-toggle/configuration-revision-service.ts | 4 ++++ src/lib/middleware/response-time-metrics.test.ts | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c5e36bbe1b..98e7946cbd 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "prebuild:watch": "yarn run clean", "build:watch": "tsc -w --strictNullChecks false", "prebuild": "yarn run clean", - "test": "NODE_ENV=test PORT=4243 jest", + "test": "NODE_ENV=test PORT=4243 node --trace-warnings node_modules/.bin/jest", "test:unit": "NODE_ENV=test PORT=4243 jest --testPathIgnorePatterns=src/test/e2e --testPathIgnorePatterns=dist", "test:docker": "./scripts/docker-postgres.sh", "test:report": "NODE_ENV=test PORT=4243 jest --reporters=\"default\" --reporters=\"jest-junit\"", diff --git a/src/lib/features/feature-toggle/configuration-revision-service.ts b/src/lib/features/feature-toggle/configuration-revision-service.ts index ca9bf072c1..49b11ceeb5 100644 --- a/src/lib/features/feature-toggle/configuration-revision-service.ts +++ b/src/lib/features/feature-toggle/configuration-revision-service.ts @@ -78,4 +78,8 @@ export default class ConfigurationRevisionService extends EventEmitter { return this.revisionId; } + + destroy(): void { + ConfigurationRevisionService.instance?.removeAllListeners(); + } } diff --git a/src/lib/middleware/response-time-metrics.test.ts b/src/lib/middleware/response-time-metrics.test.ts index 38a459f492..d5fe37c556 100644 --- a/src/lib/middleware/response-time-metrics.test.ts +++ b/src/lib/middleware/response-time-metrics.test.ts @@ -52,6 +52,9 @@ describe('responseTimeMetrics old behavior', () => { getAppCountSnapshot: jest.fn(), }; const eventBus = new EventEmitter(); + afterEach(() => { + eventBus.removeAllListeners(); + }); test('uses baseUrl and route path to report metrics', async () => { let timeInfo: any;