[FIX] bus: mock websocket worker during tests
The bus service communicates with the websocket worker in order to deliver messages coming from the bus. Most of the time, it is using a `SharedWorker` but when this kind of worker is not available, it fallbacks on a `Worker`. During tests, the `SharedWorker` is mocked to prevent opening websockets on the server. However, the `Worker` is not. Since [1], opening websockets during tests is forbidden thus, any test relying on `Worker` and expecting a connection to be established would fail. This PR solves this issue by mocking the `Worker` constructor as well. [1]: https://github.com/odoo/odoo/pull/141519 closes odoo/odoo#143988 X-original-commit: 1c67542decb1b6ad38fd15532cb6ac82417ca6dc Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com> Signed-off-by: Matthieu Stockbauer (tsm) <tsm@odoo.com>
This commit is contained in:
@@ -55,6 +55,14 @@ class SharedWorkerMock extends EventTarget {
|
||||
}
|
||||
}
|
||||
|
||||
class WorkerMock extends SharedWorkerMock {
|
||||
constructor(websocketWorker) {
|
||||
super(websocketWorker);
|
||||
this.port.start();
|
||||
this.postMessage = this.port.postMessage.bind(this.port);
|
||||
}
|
||||
}
|
||||
|
||||
let websocketWorker;
|
||||
/**
|
||||
* @param {*} params Parameters used to patch the websocket worker.
|
||||
@@ -81,6 +89,14 @@ export function patchWebsocketWorkerWithCleanup(params = {}) {
|
||||
});
|
||||
return sharedWorker;
|
||||
},
|
||||
Worker: function () {
|
||||
const worker = new WorkerMock(websocketWorker);
|
||||
registerCleanup(() => {
|
||||
worker._messageChannel.port1.close();
|
||||
worker._messageChannel.port2.close();
|
||||
});
|
||||
return worker;
|
||||
},
|
||||
});
|
||||
registerCleanup(() => {
|
||||
if (websocketWorker) {
|
||||
|
||||
Reference in New Issue
Block a user