Skip to content

Commit

Permalink
Merge pull request #199 from stereobooster/112-timeouts
Browse files Browse the repository at this point in the history
[#112] List what timed out
  • Loading branch information
stereobooster authored Jun 20, 2018
2 parents 70418c2 + 98305b6 commit d5a78a2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn-error.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ key is `urls`. Other optional options are:
* `puppeteerArgs` - Args sent to puppeteer when launching. [List
of strings for headless Chrome](https://peter.sh/experiments/chromium-command-line-switches/).
* `cssoOptions` - CSSO compress function [options](https://github.com/css/csso#compressast-options)
* `timeout` - Maximum navigation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.

## Warnings

Expand Down
22 changes: 21 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const csso = require('csso');
const csstree = require('css-tree');
const cheerio = require('cheerio');
const utils = require('./utils');
const { createTracker } = require('./tracker');
const url = require('url');

const isOk = response => response.ok() || response.status() === 304;
Expand Down Expand Up @@ -124,8 +125,20 @@ const processPage = ({
// a second time.
let fulfilledPromise = false;

const tracker = createTracker(page);
const safeReject = error => {
if (!fulfilledPromise) {
if (error.message.startsWith('Navigation Timeout Exceeded')) {
const urls = tracker.urls();
if (urls.length > 1) {
error.message += `\nTracked URLs that have not finished: ${urls.join(
', '
)}`;
} else if (urls.length > 0) {
error.message += `\nFor ${urls[0]}`;
}
}
tracker.dispose();
reject(error);
}
};
Expand All @@ -146,6 +159,10 @@ const processPage = ({
await page.setViewport(options.viewport);
}

if (options.timeout !== undefined) {
page.setDefaultNavigationTimeout(options.timeout);
}

// A must or else you can't do console.log from within page.evaluate()
page.on('console', msg => {
if (debug) {
Expand Down Expand Up @@ -311,7 +328,10 @@ const processPage = ({
allHrefs.push(href);
});

if (!fulfilledPromise) resolve();
if (!fulfilledPromise) {
tracker.dispose();
resolve();
}
} catch (e) {
return safeReject(e);
}
Expand Down
26 changes: 26 additions & 0 deletions src/tracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Sets up event listeners on the Browser.Page instance to maintain a set
* of URLs that have started but never finished or failed.
*
* @param {Object} page
* @return Object
*/

const createTracker = page => {
const requests = new Set();
const onStarted = request => requests.add(request);
const onFinished = request => requests.delete(request);
page.on('request', onStarted);
page.on('requestfinished', onFinished);
page.on('requestfailed', onFinished);
return {
urls: () => Array.from(requests).map(r => r.url()),
dispose: () => {
page.removeListener('request', onStarted);
page.removeListener('requestfinished', onFinished);
page.removeListener('requestfailed', onFinished);
}
};
};

module.exports = { createTracker };
11 changes: 11 additions & 0 deletions tests/examples/with-timeout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="timeout.css?1" rel="stylesheet">
<link href="timeout.css?2" rel="stylesheet">
</head>
<body>
<p>Only a p tag here.</p>
</body>
</html>
30 changes: 30 additions & 0 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ fastify.get('/307.html', (req, reply) => {
reply.redirect(307, '/redirected.html');
});

fastify.get('/timeout.html', (req, reply) => {
setTimeout(() => reply.send('timeout'), 300);
});

fastify.get('/timeout.css', (req, reply) => {
setTimeout(() => reply.send('timeout'), 300);
});

let browser;

const runMinimalcss = (path, options = {}) => {
Expand Down Expand Up @@ -216,3 +224,25 @@ test('accept CSSO options', async () => {
({ finalCss } = await runMinimalcss('comments', { cssoOptions }));
expect(finalCss).not.toMatch('test css comment');
});

test('timeout error for page', async () => {
expect.assertions(2);
try {
await runMinimalcss('timeout', { timeout: 200 });
} catch (e) {
expect(e.message).toMatch('Navigation Timeout Exceeded: 200ms exceeded');
expect(e.message).toMatch('For http://localhost:3000/timeout.html');
}
});

test('timeout error for resources', async () => {
expect.assertions(2);
try {
await runMinimalcss('with-timeout', { timeout: 200 });
} catch (e) {
expect(e.message).toMatch('Navigation Timeout Exceeded: 200ms exceeded');
expect(e.message).toMatch(
'Tracked URLs that have not finished: http://localhost:3000/timeout.css?1, http://localhost:3000/timeout.css?2'
);
}
});

0 comments on commit d5a78a2

Please sign in to comment.