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

tests: add smoketest for slow service worker #6648

Merged
merged 2 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
6 changes: 5 additions & 1 deletion lighthouse-cli/test/fixtures/offline-ready-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ const RUNTIME = 'runtime';

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
self.skipWaiting();
if (self.location.search.includes('slow')) {
event.waitUntil(new Promise(resolve => setTimeout(resolve, 5000)));
} else {
self.skipWaiting();
}

const populateCaches = caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS));
Expand Down
22 changes: 18 additions & 4 deletions lighthouse-cli/test/fixtures/offline-ready.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,32 @@ <h4>
</h4>

<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/offline-ready-sw.js').then(function(registration) {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

async function registerServiceWorker(qs = '') {
if (!('serviceWorker' in navigator)) return

try {
const registration = await navigator.serviceWorker.register(`/offline-ready-sw.js${qs}`)
console.log('service worker registration complete');

registration.addEventListener('statechange', e => {
console.log('sw registration is now', e.target.state);
});
}).catch(function(e) {
} catch (e) {
console.error('service worker is not so cool.', e);
throw e;
Copy link
Member

Choose a reason for hiding this comment

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

do we check for this anywhere? Or is the hope that a smoke test will fail and this would be used for debugging.

Maybe we should be pulling in errors-in-console into all our smoke tests :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

do we check for this anywhere?

nope

Or is the hope that a smoke test will fail and this would be used for debugging.

Bingo!

Maybe we should be pulling in errors-in-console into all our smoke tests :)

Not a bad idea, followup! :D

});
}
}

if (window.location.search.includes('slow')) {
setTimeout(() => registerServiceWorker('?delay=5000&slow'), 500);
Copy link
Collaborator

Choose a reason for hiding this comment

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

would it make any difference to do this on "load", where the load event is deferred by a significant amount of time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This setTimeout is mostly useless I can actually remove it since it's complicating the understanding of what's going on. Switching to fire on the load event is a good idea 👍

the load event is currently being delayed by an image farther down that takes 2s, the only way LH doesn't catch service worker AFAICT is if the page doesn't call .register quickly enough, and this test was meant to prove that fact.

Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAIK this (registering SW on "load") is basically what pintrest.com (which LH says has no SW) does. not sure if the key difference is how long until "load" fires - or something else entirely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If that's all they're doing, then I'm not sure what's going on there. We always wait until load so unless their load event handlers take a very long time such that we don't get to .register in time, then it must be something else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can take a look closer at pinterest if there's not already too many cooks in the kitchen :)

Copy link
Collaborator

@connorjclark connorjclark Nov 26, 2018

Choose a reason for hiding this comment

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

There's at most 1 :) IDK if @wardpeet ever started on it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Haha, alright I'll poke around.

} else {
registerServiceWorker();
}

</script>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,17 @@ module.exports = [
},
},
},

{
requestedUrl: 'http://localhost:10503/offline-ready.html?slow',
finalUrl: 'http://localhost:10503/offline-ready.html?slow',
audits: {
'service-worker': {
score: 1,
},
'works-offline': {
score: 1,
},
},
},
];