use session.poll as well for polling when receiving a message

This commit is contained in:
Bruno Windels 2019-04-16 11:55:33 +02:00
parent 9610e9b57e
commit 20c3023b94
3 changed files with 14 additions and 19 deletions

View file

@ -202,13 +202,12 @@ module.exports = class RiotSession {
return this.browser.close();
}
async poll(callback, timeout) {
const INTERVAL = 100;
async poll(callback, timeout, interval = 100) {
let waited = 0;
while(waited < timeout) {
await this.delay(INTERVAL);
waited += INTERVAL;
if (callback()) {
await this.delay(interval);
waited += interval;
if (await callback()) {
return true;
}
}

View file

@ -65,7 +65,7 @@ module.exports = async function signup(session, username, password, homeserver)
//wait for registration to finish so the hash gets set
//onhashchange better?
const foundHomeUrl = await session.poll(() => {
const foundHomeUrl = await session.poll(async () => {
const url = session.page.url();
return url === session.url('/#/home');
}, 5000);

View file

@ -52,22 +52,18 @@ module.exports.receiveMessage = async function(session, expectedMessage) {
return getMessageFromEventTile(lastTile);
}
let lastMessage = null;
let isExpectedMessage = false;
let totalTime = 0;
while (!isExpectedMessage) {
let lastMessage;
await session.poll(async () => {
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");
} catch(err) {
return false;
}
totalTime += 200;
await session.delay(200);
}
// stop polling when found the expected message
return lastMessage &&
lastMessage.body === expectedMessage.body &&
lastMessage.sender === expectedMessage.sender;
}, 5000, 200);
assertMessage(lastMessage, expectedMessage);
session.log.done();
}