From 4d00cedacab69fbc67483cd102486143d16351bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Tue, 5 Dec 2023 12:12:54 +0100 Subject: [PATCH] [FIX] mail: fix race condition in "make voice message in chat" runbot-46626 closes odoo/odoo#144901 Signed-off-by: Didier Debondt (did) --- .../voice_message/voice_message_tests.js | 21 +++++++++++++++---- addons/web/static/tests/helpers/utils.js | 5 +++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/addons/mail/static/tests/discuss/voice_message/voice_message_tests.js b/addons/mail/static/tests/discuss/voice_message/voice_message_tests.js index 600cea341c3..3219621cf46 100644 --- a/addons/mail/static/tests/discuss/voice_message/voice_message_tests.js +++ b/addons/mail/static/tests/discuss/voice_message/voice_message_tests.js @@ -141,7 +141,6 @@ QUnit.test("make voice message in chat", async () => { return super.fetchFile(url("/mail/static/src/audio/call_02_in_.mp3")); }, }); - patchDate(2023, 6, 31, 13, 0, 0); mockGetMedia(); const cleanUp = patchAudio(); const pyEnv = await startServer(); @@ -155,11 +154,25 @@ QUnit.test("make voice message in chat", async () => { }); const { openDiscuss } = await start(); openDiscuss(channelId); + await contains("button[title='Voice Message']"); + patchDate(2023, 6, 31, 13, 0, 0, 0); await click("button[title='Voice Message']"); await contains(".o-mail-VoiceRecorder", { text: "00 : 00" }); - await Promise.resolve(); - // simulate 10 sec elapsed - patchDate(2023, 6, 31, 13, 0, 11); // +1 so exactly 10 sec elapsed + /** + * Simulate 10 sec elapsed. + * `patchDate` does not freeze the time, it merely changes the value of "now" at the time it was + * called. The code of click following the first `patchDate` doesn't actually happen at the time + * that was specified, but few miliseconds later (8 ms on my machine). + * The process following the next `patchDate` is intended to be between 10s and 11s later than + * the click, because the test wants to assert a 10 sec counter, and the two dates are + * substracted and then rounded down in the code (it means absolute values are irrelevant here). + * The problem with aiming too close to a 10s difference is that if the click is longer than + * the following process, it will round down to 9s. + * The problem with aiming too close to a 11s difference is that if the click is shorter than + * the following process, it will round down to 11s. + * The best bet is therefore to use 10s + 500ms difference. + */ + patchDate(2023, 6, 31, 13, 0, 10, 500); // simulate some microphone data audioProcessor.process([[new Float32Array(128)]]); await contains(".o-mail-VoiceRecorder", { text: "00 : 10" }); diff --git a/addons/web/static/tests/helpers/utils.js b/addons/web/static/tests/helpers/utils.js index 54e2ea9074e..79339abcec9 100644 --- a/addons/web/static/tests/helpers/utils.js +++ b/addons/web/static/tests/helpers/utils.js @@ -48,13 +48,14 @@ import { * @param {number} [hours] * @param {number} [minutes] * @param {number} [seconds] + * @param {number} [ms=0] */ -export function patchDate(year, month, day, hours, minutes, seconds) { +export function patchDate(year, month, day, hours, minutes, seconds, ms = 0) { var RealDate = window.Date; var actualDate = new RealDate(); // By default, RealDate uses the browser offset, so we must replace it with the offset fixed in luxon. - var fakeDate = new RealDate(year, month, day, hours, minutes, seconds); + var fakeDate = new RealDate(year, month, day, hours, minutes, seconds, ms); if (!(luxon.Settings.defaultZone instanceof luxon.FixedOffsetZone)) { throw new Error("luxon.Settings.defaultZone must be a FixedOffsetZone"); }