<body>
<p>Test Worker.terminate() for a worker that tries to run forever.</p>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var worker = new Worker('resources/worker-run-forever.js');

function waitForWorkerToStart() {
    var startTime = Date.now();
    function checkIfWorkerStarted() {
        if (internals.workerThreadCount == 1) {
            console.log("Worker was started");
            worker.terminate();
            setTimeout(waitForWorkerToStop, 0);

        } else if (Date.now() - startTime < 5000) {
            setTimeout(checkIfWorkerStarted, 0);

        } else {
            console.log("Worker did not show up");
            testRunner.notifyDone();
        }            
    }
    setTimeout(checkIfWorkerStarted, 0);
}

function waitForWorkerToStop() {
    var startTime = Date.now();
    function checkIfWorkerStopped() {
        if (internals.workerThreadCount == 0) {
            console.log("Worker was terminated");
            testRunner.notifyDone();

        } else if (Date.now() - startTime < 5000) {
            setTimeout(checkIfWorkerStopped, 0);

        } else {
            console.log("Did not see worker terminate");
            testRunner.notifyDone();
        }            
    }
    setTimeout(checkIfWorkerStopped, 0);
}

window.setTimeout(waitForWorkerToStart, 0);

</script>
</body>
</html>
