WorkerGlobalScope-close.html (3049B)
1 <!DOCTYPE html> 2 <title>Test WorkerGlobalScope.close functionality.</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script> 6 setup({ allow_uncaught_exception: true }); 7 8 async_test(function(t) { 9 var worker = new Worker('support/WorkerGlobalScope-close.js'); 10 worker.postMessage("typeofClose"); 11 worker.onmessage = t.step_func_done(function(evt) { 12 assert_equals(evt.data, "typeof close: function"); 13 }); 14 }, 'Test type of close function.'); 15 16 async_test(function(t) { 17 var worker = new Worker('support/WorkerGlobalScope-close.js'); 18 worker.postMessage("ping"); 19 worker.onmessage = t.step_func(function(evt) { 20 assert_equals(evt.data, "pong"); 21 // Tell the worker to close, then send a followup message. 22 worker.postMessage("close"); 23 24 // The worker may or may not be closing/closed by this call. If it is, the 25 // message won't be enqueued on the worker's event loop. If it isn't, the 26 // message will be enqueued but shouldn't be handled by the worker because 27 // the prior postMessage will cause the worker to close. In either case, 28 // the worker shouldn't postMessage back "pong". 29 // 30 // This also means that at this point we can't confidently test 31 // postMessage-ing a worker that will close nor a worker that is already 32 // closing/closed. 33 worker.postMessage("ping"); 34 35 worker.onmessage = t.step_func(function(evt) { 36 assert_not_equals(evt.data, "pong"); 37 38 // The worker should definitely be closed by now, so we can confidently 39 // test postMessage-ing a closed worker. This postMessage shouldn't throw 40 // or cause the worker to postMessage back "pong" (it shouldn't receive 41 // any events after closing). 42 worker.postMessage("ping"); 43 44 t.step_timeout(function() { t.done(); }, 500); 45 }); 46 }); 47 }, 'Test sending a message after closing.'); 48 49 async_test(function(t) { 50 var worker = new Worker('support/WorkerGlobalScope-close.js'); 51 worker.postMessage("closeWithError"); 52 worker.onerror = function(event) { 53 t.done() 54 }; 55 }, 'Test errors are delivered after close.'); 56 57 async_test(function(t) { 58 var worker = new Worker('support/WorkerGlobalScope-close.js'); 59 worker.postMessage("closeWithPendingEvents"); 60 worker.onmessage = t.step_func(function(evt) { 61 assert_unreached("Pending events should not fire: " + evt.data); 62 }); 63 worker.onerror = t.step_func(function(evt) { 64 assert_unreached("Pending events should not fire:" + evt.message); 65 }); 66 t.step_timeout(function() { t.done(); }, 500); 67 }, 'Test workers do not deliver pending events.'); 68 69 async_test(function(t) { 70 var worker = new Worker('support/WorkerGlobalScope-close.js'); 71 worker.postMessage("close"); 72 worker.onmessage = function(evt) { 73 assert_equals(evt.data, "Should be delivered"); 74 // Give worker a chance to close first, then terminate it. 75 t.step_timeout(function() { 76 worker.terminate(); 77 t.done(); 78 }, 500); 79 }; 80 }, 'Test terminating a worker after close.'); 81 </script>