Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_8_5 / extensions / worker-terminate.js
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * Any copyright is dedicated to the Public Domain.
4  * http://creativecommons.org/licenses/publicdomain/
5  * Contributor: Jason Orendorff <jorendorff@mozilla.com>
6  */
7
8 if (typeof Worker != 'undefined') {
9     JSTest.waitForExplicitFinish();
10
11     // This tests that a parent worker can terminate a child.  We run the test
12     // several times serially. If terminate() doesn't work, the runaway Workers
13     // will soon outnumber the number of threads in the thread pool, and we
14     // will deadlock.
15     var i = 0;
16
17     function next() {
18         if (++i == 10) {
19             reportCompare(0, 0, "worker-terminate");
20             JSTest.testFinished();
21             return;
22         }
23
24         var w = new Worker(workerDir + "worker-terminate-child.js");
25         w.onmessage = function (event) {
26             reportCompare("killed", event.data, "killed runaway worker #" + i);
27             next();
28         };
29         w.onerror = function (event) {
30             reportCompare(0, 1, "Got error: " + event.message);
31         };
32         w.postMessage(workerDir);
33     }
34     next();
35 } else {
36     reportCompare(0, 0, "Test skipped. Shell workers required.");
37 }