2 <p>Test WorkerGlobalScope.close() method. Will print PASS multiple times, followed by DONE.
</p>
4 <script src=
"resources/worker-util.js"></script>
6 if (window
.testRunner
) {
7 testRunner
.dumpAsText();
8 testRunner
.waitUntilDone();
9 waitUntilWorkerThreadsExit(runTests
);
11 log("NOTE: Test relies on window.testRunner to detect when worker threads have exited. Please run this test via DumpRenderTree");
12 waitUntilWorkerThreadsExit(runTests
);
17 function startWorker()
19 if (window
.testRunner
)
20 log("PASS: workerThreadCount = " + internals
.workerThreadCount
);
21 worker
= new Worker('resources/worker-close.js');
26 log("Test 1: Call close() in the worker, then enter infinite loop. From the Worker object side, call terminate(). Verify the loop is terminated.")
28 worker
.postMessage("close_post_loop"); // Close the worker but put it immediately into infinite loop.
29 worker
.onmessage = function(event
) {
30 if (window
.testRunner
) {
31 log("PASS: Worker thread created and closed, received message: " + event
.data
);
32 // Terminate the worker.
34 waitUntilWorkerThreadsExit(testCloseWithAccumulatedMessages
);
39 function testCloseWithAccumulatedMessages()
41 log("PASS: Worker exited after terminate() terminated the infinite loop.");
42 log("Test 2: post 2 messages to a MessagePort into the worker. port.onmessage handler inside the worker will call close(). Verify that the second onmessage was not executed.")
44 var channel
= new MessageChannel();
45 worker
.onmessage = function(event
) {
46 if (event
.data
== "echo_ping2")
47 log("FAIL: The MessagePort message was dispatched after a JS fragment that called 'close()' exited.");
49 log("PASS: MessagePort multiple message test, received message: " + event
.data
);
50 setTimeout("waitUntilWorkerThreadsExit(workerExited)", 100); // Let it run a bit to catch the echo_ping2 if it arrives.
52 worker
.postMessage("take_port", [channel
.port2
]);
55 channel
.port1
.postMessage("ping1");
56 channel
.port1
.postMessage("ping2");
58 // Now enable the messages to be delivered. Only first should echo back.
59 worker
.postMessage("start_port");
62 function workerExited()
64 log("PASS: MessagePort messages were not delivered asfter close().");