2 <p>Test WorkerContext.close functionality. Should print a series of PASS messages, followed with DONE.
</p>
7 document
.getElementById("result").innerHTML
+= message
+ "<br>";
10 if (window
.testRunner
)
12 testRunner
.dumpAsText();
13 testRunner
.waitUntilDone();
16 var worker
= new Worker('resources/worker-close.js');
19 worker
.postMessage("typeofClose");
20 worker
.onmessage
= testTypeofClose
;
22 function testTypeofClose(evt
)
24 if (evt
.data
== "typeof close: function")
25 log("PASS: " + evt
.data
);
27 log("FAIL: " + evt
.data
);
28 worker
.onmessage
= testMessageAfterClose
;
29 worker
.postMessage("ping");
32 function testMessageAfterClose(evt
) {
33 if (evt
.data
== "pong")
34 log("PASS: received message before close");
36 log("FAIL: received unknown response: " + evt
.data
);
38 // Tell the worker to close, then send a followup message. This message should not be delivered,
39 // since that would require JS to invoke the onmessage handler, which does not happen after the JS
40 // fragment with 'close()' in it exits. So, the 'ping' should not come back as 'pong'.
41 worker
.postMessage("close");
42 worker
.postMessage("ping");
43 worker
.onmessage = function(evt
) {
44 if (evt
.data
!= "pong") {
45 log("PASS: Received message posted right after close() was invoked: " + evt
.data
);
46 timeout
= setTimeout(testErrorAfterClose
, 1000);
48 log("FAIL: Received a message originated from a handler in the worker after the JS fragment with close() exited" + evt
.data
);
54 function testErrorAfterClose()
56 log("PASS: no messages arrive to worker after JS fragment with close() exits");
57 // Test that errors are delivered after close.
58 worker
= new Worker('resources/worker-close.js');
59 worker
.postMessage("closeWithError");
60 worker
.onerror = function(event
) {
61 log("PASS: Error arrived after close: " + event
.message
);
67 function testPendingEvents()
69 // Now test that workers do not deliver pending events
70 worker
= new Worker('resources/worker-close.js');
71 worker
.postMessage("closeWithPendingEvents");
72 worker
.onmessage = function(evt
) {
73 log("FAIL: pending events should not fire:" + evt
.data
);
76 worker
.onerror = function(evt
) {
77 log("FAIL: pending events should not fire:" + evt
.message
);
80 timeout
= setTimeout(testTerminateAfterClose
, 500);
83 function testTerminateAfterClose()
85 log("PASS: close() did not dispatch pending events");
86 worker
= new Worker('resources/worker-close.js');
87 worker
.postMessage("close");
88 worker
.onmessage = function(evt
) {
89 log("PASS: Received message after worker closed: " + evt
.data
);
90 // Give worker a chance to close first, then terminate it.
91 timeout
= setTimeout(function() {
100 clearTimeout(timeout
);
102 if (window
.testRunner
)
103 testRunner
.notifyDone();