Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise "close" event when worker processes terminate #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ http://www.whatwg.org/specs/web-workers/current-work/
worker.terminate();
});

// Optional, if you want to be notified when a worker child exits
worker.addListener('close', function(code) {
sys.puts('Worker exited with exit code ' + code);
});

## Example Worker File

var worker = require("worker").worker;
Expand Down
4 changes: 4 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ worker.onmessage = function (msg) {
worker.addListener("message", function (msg) {
sys.puts(msg.hello);
worker.terminate();
});

worker.addListener('close', function(code) {
sys.puts('Worker exited with exit code ' + code);
});
1 change: 1 addition & 0 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function WorkerChild (eventDest, filename) {

this.child.addListener("exit", function (code) {
debug(self.child.pid + ": exit "+code);
self.eventDest.emit("close", code);
});

this.buffer = "";
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ worker.onmessage = function (msg) {
if(msg.error) {
throw("ErrorMarker");
}

if(msg.exitCode) {
process.exit(msg.exitCode);
return;
}

msg.output = msg.input * 3;
setTimeout(function () {
Expand Down
39 changes: 31 additions & 8 deletions test/test-worker.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var common = require("./common");
var sys = require("sys");
var assert = require('assert');

var Worker = require("../lib/worker").Worker;

process.ENV["NODE_PATH"] = common.libDir;

function makeWorker (filename) {
return new Worker(__dirname+"/fixtures/"+filename);
}

// basic test
var worker = makeWorker("worker.js");

worker.onmessage = function (msg) {
if (msg.input) {
assert.ok(msg.output == msg.input * 3, "We can multiply asyncly");
Expand All @@ -21,7 +21,7 @@ worker.onmessage = function (msg) {
}
}
};

worker.postMessage({
input: 1
});
Expand All @@ -38,7 +38,7 @@ worker.postMessage({
// error handling
setTimeout(function () {
setTimeout(function () {

var w2 = makeWorker("worker.js");
w2.postMessage({
error: true
Expand All @@ -51,7 +51,7 @@ setTimeout(function () {
assert.ok(false, "Wanted an error, but got a message");
w2.terminate();
});

var w3 = makeWorker("worker.js");
w3.postMessage({
error: true
Expand All @@ -64,7 +64,7 @@ setTimeout(function () {
assert.ok(false, "Wanted an error, but got a message");
w3.terminate();
});

}, 10);
}, 10);

Expand Down Expand Up @@ -112,4 +112,27 @@ waitWorker.postMessage({
waitWorker.addListener("message", function () {
assert.ok(true, "Worker response can be async.")
waitWorker.terminate();
});

// Test workers raise "close" event when they exit
var workersClosed = 0;
var exitWorker = makeWorker('worker.js');
exitWorker.postMessage({
exitCode: 1,
});
exitWorker.addListener('close', function(code) {
assert.ok(code === 1, 'Exited worker returns exit code');
++workersClosed;
});

// Test that when a worker raises a syntax error, it raises a close event
var exitWorker = makeWorker('syntax-error-worker.js');
exitWorker.onerror = function() { };
exitWorker.addListener('close', function(code) {
assert.ok(true, 'Worker with syntax error raises close event');
++workersClosed;
});

process.addListener('exit', function () {
assert.ok(workersClosed == 2, "Exited workers raise 'close' event");
});