From 3571d11d7c5201b454c2f3365fae04931ff6c03a Mon Sep 17 00:00:00 2001 From: Hiroki Nakagawa Date: Tue, 13 Nov 2018 04:59:29 +0000 Subject: [PATCH] ServiceWorker: Deflake update-with-script-type.https.html To sheriff: Please re-disable the test if it's still flaky. In this test file, some tests start re-registering the second service worker before the first service worker gets activated. In my theory, this sometimes squashes the second registration job into the first registration job, and results in test flakiness. This CL makes sure the second register runs after the first service worker gets activated. Note that it would be the correct behavior that the second registration job isn't squashed when the script type is changed. There is a spec issue about this: https://github.com/w3c/ServiceWorker/issues/1358 Bug: 901317 Change-Id: I7ce379071a35ef9aeb98e4492d651ee6fc4714ec Reviewed-on: https://chromium-review.googlesource.com/c/1328546 Reviewed-by: Makoto Shimazu Commit-Queue: Hiroki Nakagawa Cr-Commit-Position: refs/heads/master@{#607494} --- .../update-registration-with-type.https.html | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/service-workers/service-worker/update-registration-with-type.https.html b/service-workers/service-worker/update-registration-with-type.https.html index 0f50ae20b971cf..b712c30f71e9a8 100644 --- a/service-workers/service-worker/update-registration-with-type.https.html +++ b/service-workers/service-worker/update-registration-with-type.https.html @@ -16,16 +16,17 @@ const script = `resources/update-registration-with-type.py?classic_first=1&key=${key}`; const scope = 'resources/update-registration-with-type'; await service_worker_unregister(t, scope); + t.add_cleanup(() => service_worker_unregister(t, scope)); // Register with classic script type. const firstRegistration = await navigator.serviceWorker.register(script, { scope: scope, type: 'classic' }); - firstRegistration.installing.postMessage(' '); - let msgEvent = await new Promise(resolve => { - navigator.serviceWorker.onmessage = resolve; - }); + const firstWorker = firstRegistration.installing; + await wait_for_state(t, firstWorker, 'activated'); + firstWorker.postMessage(' '); + let msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r); assert_equals(msgEvent.data, 'A classic script.'); // Re-register with module script type. @@ -33,11 +34,12 @@ scope: scope, type: 'module' }); - secondRegistration.installing.postMessage(' '); - msgEvent = await new Promise(resolve => { - navigator.serviceWorker.onmessage = resolve; - }); + const secondWorker = secondRegistration.installing; + secondWorker.postMessage(' '); + msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r); assert_equals(msgEvent.data, 'A module script.'); + + assert_not_equals(firstWorker, secondWorker); assert_equals(firstRegistration, secondRegistration); }, 'Update the registration with a different script type (classic => module).'); @@ -46,16 +48,17 @@ const script = `resources/update-registration-with-type.py?classic_first=0&key=${key}`; const scope = 'resources/update-registration-with-type'; await service_worker_unregister(t, scope); + t.add_cleanup(() => service_worker_unregister(t, scope)); // Register with module script type. const firstRegistration = await navigator.serviceWorker.register(script, { scope: scope, type: 'module' }); - firstRegistration.installing.postMessage(' '); - let msgEvent = await new Promise(resolve => { - navigator.serviceWorker.onmessage = resolve; - }); + const firstWorker = firstRegistration.installing; + await wait_for_state(t, firstWorker, 'activated'); + firstWorker.postMessage(' '); + let msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r); assert_equals(msgEvent.data, 'A module script.'); // Re-register with classic script type. @@ -63,11 +66,12 @@ scope: scope, type: 'classic' }); - secondRegistration.installing.postMessage(' '); - msgEvent = await new Promise(resolve => { - navigator.serviceWorker.onmessage = resolve; - }); + const secondWorker = secondRegistration.installing; + secondWorker.postMessage(' '); + msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r); assert_equals(msgEvent.data, 'A classic script.'); + + assert_not_equals(firstWorker, secondWorker); assert_equals(firstRegistration, secondRegistration); }, 'Update the registration with a different script type (module => classic).'); @@ -84,18 +88,17 @@ scope: scope, type: 'classic' }); - await wait_for_state(t, firstRegistration.installing, 'activated'); - const firstActiveWorker = firstRegistration.active; + const firstWorker = firstRegistration.installing; + await wait_for_state(t, firstWorker, 'activated'); // Re-register with module script type. const secondRegistration = await navigator.serviceWorker.register(script, { scope: scope, type: 'module' }); - await wait_for_state(t, secondRegistration.installing, 'activated'); - const secondActiveWorker = secondRegistration.active; + const secondWorker = secondRegistration.installing; - assert_not_equals(firstActiveWorker, secondActiveWorker); + assert_not_equals(firstWorker, secondWorker); assert_equals(firstRegistration, secondRegistration); }, 'Update the registration with a different script type (classic => module) ' + 'and with a same main script.'); @@ -111,18 +114,17 @@ scope: scope, type: 'module' }); - await wait_for_state(t, firstRegistration.installing, 'activated'); - const firstActiveWorker = firstRegistration.active; + const firstWorker = firstRegistration.installing; + await wait_for_state(t, firstWorker, 'activated'); // Re-register with classic script type. const secondRegistration = await navigator.serviceWorker.register(script, { scope: scope, type: 'classic' }); - await wait_for_state(t, secondRegistration.installing, 'activated'); - const secondActiveWorker = secondRegistration.active; + const secondWorker = secondRegistration.installing; - assert_not_equals(firstActiveWorker, secondActiveWorker); + assert_not_equals(firstWorker, secondWorker); assert_equals(firstRegistration, secondRegistration); }, 'Update the registration with a different script type (module => classic) ' + 'and with a same main script.'); @@ -168,6 +170,7 @@ type: 'classic' }); assert_not_equals(firstRegistration.installing, null); + await wait_for_state(t, firstRegistration.installing, 'activated'); // Re-register with module script type and expect TypeError. return promise_rejects(t, new TypeError, navigator.serviceWorker.register(script, { @@ -192,6 +195,7 @@ type: 'module' }); assert_not_equals(firstRegistration.installing, null); + await wait_for_state(t, firstRegistration.installing, 'activated'); // Re-register with classic script type and expect TypeError. return promise_rejects(t, new TypeError, navigator.serviceWorker.register(script, {