Layout Test http/tests/workers/service/postmessage-after-sw-process-crash.https.html...
[WebKit-https.git] / LayoutTests / http / tests / workers / service / resources / postmessage-after-sw-process-crash.js
1 let serviceWorkerHasReceivedState = false;
2 let worker = null;
3 let remainingAttempts = 1000; // We try for 10 seconds before timing out.
4
5 navigator.serviceWorker.addEventListener("message", function(event) {
6     if (!serviceWorkerHasReceivedState) {
7         if (!event.data) {
8             log("FAIL: service worker did not receive the state");
9             finishSWTest();
10             return;
11         }
12         serviceWorkerHasReceivedState = true;
13
14         log("* Simulating Service Worker process crash");
15         testRunner.terminateServiceWorkerProcess();
16
17         handle = setInterval(function() {
18             remainingAttempts--;
19             if (!remainingAttempts) {
20                 log("FAIL: service worker did not respond after process termination");
21                 clearInterval(handle);
22                 finishSWTest();
23                 return;
24             }
25
26             worker.postMessage("HasState");
27         }, 10);
28         return;
29     }
30
31     // Worker still has the state, it was not terminated yet.
32     if (event.data === serviceWorkerHasReceivedState)
33         return;
34
35     log("PASS: service worker lost the state and responded the postMessage after process termination");
36     clearInterval(handle);
37     finishSWTest();
38 });
39
40 navigator.serviceWorker.register("resources/postmessage-after-sw-process-crash-worker.js", { }).then(function(registration) {
41     worker = registration.installing;
42     log("* Sending State to Service Worker");
43     worker.postMessage("SetState");
44     log("* Asking Service Worker if it received the state");
45     worker.postMessage("HasState");
46 });