<script>
function test()
{
    if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    worker = new Worker("resources/worker-common.js");

    // The setTimeout is generally a bad idea in tests, but this one is here because the test is trying to call terminate() in the time
    // window after loading the worker's script (and creation of worker process in case of Chromium) but before the initial script is executed.
    // The crash happened when JS engine in worker is initialized, so the timeout here is approximate. JS initialization in Chromium case
    // is quite long for the process-wide instance of V8, especially in debug builds - so this test hits the crash quite often.
    // The test is inherently flakey because of it but on a 'false positive' side, meaning if the timeout is not enough the test is SUCCESS
    // so there are no annoying failures.
    setTimeout(continueTest, 100);
}

function continueTest() {
    // These messages are to let WorkerThread::workerThread go past the point of checking for termination and start initialization.
    // Since it takes time for these messages to be queued, the termination message is reaching the WorkerThread when it is already in V8 code.
    for (i = 0; i < 1000; i++)
        worker.postMessage("yada yada");

    worker.terminate();
    document.getElementById("result").innerText = "DONE";
    if (window.testRunner)
        testRunner.notifyDone();
}
</script>
<body onload=test()>
<p>This test terminates a worker at the moment before WorkerThread::workerThread starts execute the initial script. This stage includes initialization of the script engine, and V8 may crash if terminated during initialization. Test succeeds if it does not hang or crash (if worker thread is running in the separate process, that process could hang or crash).</p>
<p>This test reproduces crash specific to v8 and multi-process Chromium architecture. In case of JSC, the test is just another opportunity to exercise the Worker code.</p>
<pre id="result"></pre>
</body>
