From d72a5090d8f37f00a1ed5c7a8b52ee3b7ba69eea Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 11 Oct 2024 11:02:36 +0100 Subject: [PATCH] Add a timeout to `awaitRoomMembership` --- playwright/pages/client.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/playwright/pages/client.ts b/playwright/pages/client.ts index abd487988b..26d5509418 100644 --- a/playwright/pages/client.ts +++ b/playwright/pages/client.ts @@ -294,6 +294,8 @@ export class Client { * * This is often useful after joining a room, when we need to wait for the sync loop to catch up. * + * Times out with an error after 1 second. + * * @param roomId - ID of the room to check * @param membership - required membership. */ @@ -308,9 +310,17 @@ export class Client { cli.logger.info(`waiting for room ${roomId}: membership now ${myMembership}`); return myMembership === membership; }; - if (isReady()) return; + //if (isReady()) return; + + const timeoutPromise = new Promise((resolve) => setTimeout(resolve, 1000)).then(() => { + const room = cli.getRoom(roomId); + const myMembership = room?.getMyMembership(); + throw new Error( + `Timeout waiting for room ${roomId} membership (now '${myMembership}', wanted '${membership}')`, + ); + }); - return new Promise((resolve) => { + const readyPromise = new Promise((resolve) => { async function onEvent() { if (isReady()) { cli.removeListener(window.matrixcs.ClientEvent.Event, onEvent); @@ -320,6 +330,8 @@ export class Client { cli.on(window.matrixcs.ClientEvent.Event, onEvent); }); + + return Promise.race([timeoutPromise, readyPromise]); }, { roomId, membership }, );