Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore smoketest for offline-ready SW page #518

Merged
merged 28 commits into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
961eb29
basic sw page for smoke test. kinda fails.
paulirish Jul 18, 2016
ffa7c12
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Jul 18, 2016
cf6e085
don't log during the smoketest. makes it harder to read the travis log.
paulirish Jul 18, 2016
a2ceda2
sw logic stolen from mojibrush. thx @samccone
paulirish Jul 26, 2016
329e1a1
syntax tweaks.
paulirish Jul 26, 2016
f757c17
run smoketests on diff ports so sw isn't shared.
paulirish Jul 26, 2016
58de622
Load offline-ready page twice so we dont have to worry about racing b…
paulirish Jul 26, 2016
7c8a519
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Jul 26, 2016
cf981ba
exclude fixtures from mocha tests.
paulirish Jul 26, 2016
0a79219
exclude fixtures from coverage test.
paulirish Jul 26, 2016
610c2fd
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Jul 26, 2016
a7ad4e9
smoketest: do full default run against our basic html page (#528)
paulirish Jul 26, 2016
ca04762
Add license
paulirish Jul 26, 2016
7a1cad8
Merge branch 'offlinereadypageforsmoketest' of github.com:GoogleChrom…
paulirish Jul 26, 2016
cf0422c
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Jul 27, 2016
2366ab4
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Jul 28, 2016
e5be990
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Aug 11, 2016
83b86da
add worker from googlechrome.github.io/samples/service-worker/basic
paulirish Aug 12, 2016
92fab6f
more logging
paulirish Aug 12, 2016
71e1713
Replace pythonsimplehttpserver with a configurable node-based server
paulirish Aug 17, 2016
c6d50f3
Simpler skipWaiting thanks to https://github.com/GoogleChrome/samples…
paulirish Aug 17, 2016
9c103f6
make offline page take longer to load to allow enough time for SW to …
paulirish Aug 17, 2016
79ee76b
tests should avoid fixtures/*.js
paulirish Aug 17, 2016
9b2d520
coverage should also avoid fixtures.
paulirish Aug 17, 2016
4a820fe
Merge branch 'master' into offlinereadypageforsmoketest
paulirish Aug 20, 2016
8d04586
Set delay for image in our server.
paulirish Aug 20, 2016
2c8e0b7
license and cleanup.
paulirish Aug 20, 2016
1fed8f7
eslint-env worker, serviceworker
paulirish Aug 20, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lighthouse-cli/scripts/run-smoke-tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash

cd lighthouse-cli/test/fixtures && python -m SimpleHTTPServer 10200 &
cd lighthouse-cli/test/fixtures && python -m SimpleHTTPServer 10404 &
node lighthouse-cli/test/fixtures/static-server.js &

sleep 0.5s

NODE=$([ $(node -v | grep -E "v4") ] && echo "node --harmony" || echo "node")
config="$PWD/lighthouse-cli/test/fixtures/smoketest-offline-config.json"
Expand Down Expand Up @@ -30,10 +31,10 @@ if ! grep -q "$offline200result: false" results; then
exit 1
fi

sleep 1s
sleep 0.5s

# run minimal lighthouse run against a basic offline-sw page
$NODE lighthouse-cli --config-path=$config --quiet http://localhost:10404/offline-ready.html > results
$NODE lighthouse-cli --config-path=$config --quiet http://localhost:10503/offline-ready.html > results

if ! grep -q "$offline200result: true" results; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move it to the json response so this check isn't so ridiculous? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't as we'd have to shift this entire test to JS. another day.

echo "Fail! offline ready site did not work while offline"
Expand Down
16 changes: 4 additions & 12 deletions lighthouse-cli/test/fixtures/offline-ready-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,22 @@ const PRECACHE_URLS = [
'./smoketest-offline-config.json'
];

function pLog() {
return function(obj) {
console.log('' + Date.now() + 'I just received' + JSON.stringify(obj));
return obj;
};
}
// Names of the two caches used in this version of the service worker.
// Change to v2, etc. when you update any of the local resources, which will
// in turn trigger the install event again.
const PRECACHE = 'precache-v1';
const RUNTIME = 'runtime';



// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
var populateCaches = caches.open(PRECACHE).then(pLog())
.then(cache => cache.addAll(PRECACHE_URLS)).then(pLog());
self.skipWaiting();

const populateCaches = caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS));

event.waitUntil(populateCaches);
event.waitUntil(self.skipWaiting());
});


// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
const currentCaches = [PRECACHE, RUNTIME];
Expand Down
10 changes: 10 additions & 0 deletions lighthouse-cli/test/fixtures/offline-ready.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ <h4>
});
}
</script>

<script>
// Lighthouse will move on to the next thing at onload, however this will race with
// the cache population in the serviceworker's install phase.
// We use an image that takes 2-seconds to load to delay window onload.
// (Also about that document.write.. Bless me Souders for I have sinned...)
document.write(`
<img src="http://1.cuzillion.com/bin/resource.cgi?type=gif&sleep=2&n=1&t=${Date.now()}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this seems crazy, especially because we control our own server. Could you instead make an endpoint in static-server.js that supports this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg done.

`);
</script>
48 changes: 48 additions & 0 deletions lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need license

const http = require('http');
const path = require('path');
const fs = require('fs');
const parseURL = require('url').parse;

function requestHandler(request, response) {
const filePath = parseURL(request.url).pathname;
const absoluteFilePath = path.join(__dirname, filePath);

fs.exists(absoluteFilePath, fsExistsCallback);

function fsExistsCallback(fileExists) {
if (!fileExists) {
if (!filePath.endsWith('favicon.ico')) {
console.error(`Cannot find file ${absoluteFilePath}`);
}
return sendResponse(404, '404 - File not found');
}
fs.readFile(absoluteFilePath, 'binary', readFileCallback);
}

function readFileCallback(err, file) {
if (err) {
console.error(`Unable to read local file ${absoluteFilePath}:`, err);
return sendResponse(500, '500 - Internal Server Error');
}
sendResponse(200, file);
}

function sendResponse(statusCode, data) {
const headers = filePath.endsWith('.js') ?
{'Content-Type': 'text/javascript'} : undefined;
response.writeHead(statusCode, headers);
response.write(data, 'binary');
response.end();
}
}

const serverForOnline = http.createServer(requestHandler);
const serverForOffline = http.createServer(requestHandler);

serverForOnline.on('error', e => console.error(e.code, e));
serverForOffline.on('error', e => console.error(e.code, e));

// Listen
serverForOnline.listen(10200);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the two servers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, so they don't share a SW? That's going to make more testing like this difficult...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just need things on different ports. If a single server could listen on multiple ports, I'd do that. But at least we can share the requestHandler. :)

IMO ports and these servers are cheap and we should feel comfortable chasing #307 with this sort of setup.

serverForOffline.listen(10503);
2 changes: 1 addition & 1 deletion lighthouse-core/scripts/run-mocha.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
flag=$1

function _runmocha() {
mocha $2 $__node_harmony $(find $1/test -name '*.js') --timeout 60000;
mocha $2 $__node_harmony $(find $1/test -name '*.js' -not -path '*/fixtures/*') --timeout 60000;
}

if [ "$flag" == '--watch' ]; then
Expand Down