[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 <sad@odoo.com>
This commit is contained in:
Aaron Bohy
2022-12-23 19:19:14 +01:00
co-authored by Samuel Degueldre
parent a39e703a2e
commit 868adb0e46
+7 -8
View File
@@ -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();
});