Skip to content

Commit

Permalink
Fix race condition in puppeteer readyEvent (#740)
Browse files Browse the repository at this point in the history
* Eliminate ready event race condition

* Eliminate readyEvent race condition
  • Loading branch information
KenCoder authored and garris committed Apr 17, 2018
1 parent 03e2fb1 commit 566d77c
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions core/util/runPuppet.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,29 @@ async function processScenarioView (scenario, variantOrScenarioLabelSafe, scenar
console.log(chalk.blue('CREATING NEW REFERENCE FILE'));
}

// --- set up console output ---
// --- set up console output and ready event ---
const readyEvent = scenario.readyEvent || config.readyEvent;
let readyResolve, readyPromise;
if (readyEvent) {
readyPromise = new Promise(resolve => {
readyResolve = resolve;
});
}

page.on('console', msg => {
for (let i = 0; i < msg.args().length; ++i)
console.log(`Browser Console Log ${i}: ${msg.args()[i]}`);
for (let i = 0; i < msg.args().length; ++i) {
const line = msg.args()[i];
console.log(`Browser Console Log ${i}: ${line}`);
if (readyEvent && new RegExp(readyEvent).test(line)) {
readyResolve();
}
}
});

let chromeVersion = await page.evaluate(_ => {
let v = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return v ? parseInt(v[2], 10) : 0;
})
});

if (chromeVersion < MIN_CHROME_VERSION) {
console.warn(`***WARNING! CHROME VERSION ${MIN_CHROME_VERSION} OR GREATER IS REQUIRED. PLEASE UPDATE YOUR CHROME APP!***`);
Expand Down Expand Up @@ -112,13 +125,10 @@ async function processScenarioView (scenario, variantOrScenarioLabelSafe, scenar
await injectBackstopTools(page);

// --- WAIT FOR READY EVENT ---
var readyEvent = scenario.readyEvent || config.readyEvent;
if (readyEvent) {
await page.evaluate(`window._readyEvent = '${readyEvent}'`)
await page.evaluate(`window._readyEvent = '${readyEvent}'`);

await page.waitForFunction(() => {
return window._backstopTools.hasLogged(window._readyEvent);
});
await readyPromise;

await page.evaluate(_ => console.info('readyEvent ok'));
}
Expand Down

0 comments on commit 566d77c

Please sign in to comment.