chromium/third_party/blink/web_tests/fast/workers/worker-close-more.html

<body>
<p>Test WorkerGlobalScope.close() method. Will print PASS multiple times, followed by DONE.</p>
<div id=result></div>
<script src="resources/worker-util.js"></script>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
    waitUntilWorkerThreadsExit(runTests);
} else {
    log("NOTE: Test relies on window.testRunner to detect when worker threads have exited. Please run this test via DumpRenderTree");
    waitUntilWorkerThreadsExit(runTests);
}

var worker;

function startWorker()
{
    if (window.testRunner)
        log("PASS: workerThreadCount = " + internals.workerThreadCount);
    worker = new Worker('resources/worker-close.js');
}

function runTests()
{
    log("Test 1: Call close() in the worker, then enter infinite loop. From the Worker object side, call terminate(). Verify the loop is terminated.")
    startWorker();
    worker.postMessage("close_post_loop"); // Close the worker but put it immediately into infinite loop.
    worker.onmessage = function(event) {
        if (window.testRunner) {
            log("PASS: Worker thread created and closed, received message: " + event.data);
            // Terminate the worker.
            worker.terminate();
            waitUntilWorkerThreadsExit(testCloseWithAccumulatedMessages);
        }
    }
}

function testCloseWithAccumulatedMessages()
{
    log("PASS: Worker exited after terminate() terminated the infinite loop.");
    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.")
    startWorker();
    var channel = new MessageChannel();
    worker.onmessage = function(event) {
        if (event.data == "echo_ping2")
            log("FAIL: The MessagePort message was dispatched after a JS fragment that called 'close()' exited.");
        else
            log("PASS: MessagePort multiple message test, received message: " + event.data);
        setTimeout("waitUntilWorkerThreadsExit(workerExited)", 100); // Let it run a bit to catch the echo_ping2 if it arrives.
    }
    worker.postMessage("take_port", [channel.port2]);

    // Queue 2 messages.
    channel.port1.postMessage("ping1");
    channel.port1.postMessage("ping2");

    // Now enable the messages to be delivered. Only first should echo back.
    worker.postMessage("start_port");
}

function workerExited()
{
    log("PASS: MessagePort messages were not delivered asfter close().");
    done();
}
</script>
</body>