From 34171eab8cc1998a2301af81344cac524e3dbe22 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 2 Apr 2019 15:13:04 +0200 Subject: [PATCH] doing wait for /sync request to receive message, doesn't work well just poll every 200ms, feels way faster as before we were probably missing /sync requests --- src/session.js | 9 --------- src/usecases/timeline.js | 38 ++++++++++++++------------------------ 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/src/session.js b/src/session.js index 7ea980bd32..1363185753 100644 --- a/src/session.js +++ b/src/session.js @@ -161,15 +161,6 @@ module.exports = class RiotSession { }); } - waitForSyncResponseWith(predicate) { - return this.page.waitForResponse(async (response) => { - if (response.request().url().indexOf("/sync") === -1) { - return false; - } - return predicate(response); - }); - } - /** wait for a /sync request started after this call that gets a 200 response */ async waitForNextSuccessfulSync() { const syncUrls = []; diff --git a/src/usecases/timeline.js b/src/usecases/timeline.js index 610d1f4e9b..191117891d 100644 --- a/src/usecases/timeline.js +++ b/src/usecases/timeline.js @@ -54,31 +54,21 @@ module.exports.receiveMessage = async function(session, expectedMessage) { let lastMessage = null; let isExpectedMessage = false; - try { - lastMessage = await getLastMessage(); - isExpectedMessage = lastMessage && - lastMessage.body === expectedMessage.body && - lastMessage.sender === expectedMessage.sender; - } catch(ex) {} - // first try to see if the message is already the last message in the timeline - if (isExpectedMessage) { - assertMessage(lastMessage, expectedMessage); - } else { - await session.waitForSyncResponseWith(async (response) => { - const body = await response.text(); - if (expectedMessage.encrypted) { - return body.indexOf(expectedMessage.sender) !== -1 && - body.indexOf("m.room.encrypted") !== -1; - } else { - return body.indexOf(expectedMessage.body) !== -1; - } - }); - // wait a bit for the incoming event to be rendered - await session.delay(1000); - lastMessage = await getLastMessage(); - assertMessage(lastMessage, expectedMessage); + let totalTime = 0; + while (!isExpectedMessage) { + try { + lastMessage = await getLastMessage(); + isExpectedMessage = lastMessage && + lastMessage.body === expectedMessage.body && + lastMessage.sender === expectedMessage.sender + } catch(err) {} + if (totalTime > 5000) { + throw new Error("timed out after 5000ms"); + } + totalTime += 200; + await session.delay(200); } - + assertMessage(lastMessage, expectedMessage); session.log.done(); }