From 868adb0e46d68e74ffa4c12ee915039b4505fb2b Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 23 Dec 2022 10:38:50 +0000 Subject: [PATCH] [FIX] web: tests: clear handlers on LegacyBus Before this commit, there was a piece of code aiming at clearing event handlers registered on core.bus (the main instance of Bus), registered during a test (main use case being: there is a service, started in a test, and that service registers an event handler on core.bus). However, core.bus isn't the only instance of Bus, and this logic should be applied to all of them (e.g. web_studio.bus). This was a memory leak. This commit fixes it. X-original-commit: 2e7dd01cfdad35b3c6384fec60b158747b86ab78 Part-of: odoo/odoo#108620 Co-authored-by: Samuel Degueldre --- addons/web/static/tests/setup.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/addons/web/static/tests/setup.js b/addons/web/static/tests/setup.js index a6a9903d370..790f099083f 100644 --- a/addons/web/static/tests/setup.js +++ b/addons/web/static/tests/setup.js @@ -1,6 +1,7 @@ /** @odoo-module **/ -import core, { _t } from "web.core"; +import { _t } from "web.core"; +import LegacyBus from "web.Bus"; import session from "web.session"; import { assets, templates } from "@web/core/assets"; import { browser, makeRAMLocalStorage } from "@web/core/browser/browser"; @@ -210,16 +211,14 @@ function patchBodyAddEventListener() { }); } -function patchLegacyCoreBus() { +function patchLegacyBus() { // patch core.bus.on to automatically remove listners bound on the legacy bus // during a test (e.g. during the deployment of a service) - const originalOn = core.bus.on.bind(core.bus); - const originalOff = core.bus.off.bind(core.bus); - patchWithCleanup(core.bus, { + patchWithCleanup(LegacyBus.prototype, { on() { - originalOn(...arguments); + this._super(...arguments); registerCleanup(() => { - originalOff(...arguments); + this.off(...arguments); }); }, }); @@ -359,7 +358,7 @@ export async function setupTests() { cleanLoadedLanguages(); patchBrowserWithCleanup(); patchBodyAddEventListener(); - patchLegacyCoreBus(); + patchLegacyBus(); patchOdoo(); patchSessionInfo(); });