Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
updated testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Nov 26, 2011
1 parent d67b570 commit 387b1f5
Show file tree
Hide file tree
Showing 5 changed files with 677 additions and 0 deletions.
219 changes: 219 additions & 0 deletions test/simple/test-cluster-auto-fork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.


var common = require('../test/common');
var assert = require('assert');
var cluster = require('cluster');
var os = require("os");

function forEach(obj, fn) {
Object.keys(obj).forEach(function (name, index) {
fn(obj[name], name, index);
});
}

if (cluster.isWorker) {

//Throw an error at some point
cluster.worker.on('message', function (message) {
if (message === "please throw error") {
process.exit(0);
}
});

//Just keep the worker alive
var http = require('http');
http.Server(function () {

}).listen(common.PORT, "127.0.0.1");
}

else if (cluster.isMaster) {

var checks = {
defaultWorkers: false,
uniqueIDincrease: false,
workerIDincrease: false,
workerIDreuse: false,
workerIDrespawnReuse: false,
onlineWorkers: false,
startup: false,
autoRespawn: false,
manualRespawn: false,
suicideException: false
};

var cpus = os.cpus().length;
var workers = 0;
var workerID = -1;
var uniqueID = 0;
var onlineWorkers = 0;
var listeningWorkers = 0;
var forkUsed = false;

//Incresement listenters
cluster.on('fork', function () {
uniqueID += 1;
});

cluster.on('online', function () {
onlineWorkers += 1;
});

cluster.on('listening', function () {
listeningWorkers += 1;
});

cluster.on('death', function () {
onlineWorkers -= 1;
listeningWorkers -= 1;
});


//This listenter will be removed again
cluster.on('fork', function forkListenter (worker) {

workers += 1;
workerID += 1;

if (workers === cpus) {
forkUsed = true;
var time = Date.now();

//Check the worerID incresement
checks.workerIDincrease = (workerID === worker.workerID);

//Check the statup time, accept 1 sec delay
checks.startup = (worker.startup >= (time - 1000) && worker.startup <= time);

cluster.removeListener('fork', forkListenter);
}

});

//This listenter will be removed again
cluster.on('online', function onlineListenter () {

//Check that the number of default workers are cpus
checks.defaultWorkers = ((cluster.workers.length - 1) === cpus);

//Check onlineWorkers
checks.onlineWorkers = (onlineWorkers === cluster.onlineWorkers);

if (onlineWorkers === workers) {
cluster.removeListener('online', onlineListenter);
}
});

//This listenter will be removed again
cluster.on('listening', function listeningListenter (newWorker) {

//next step begin kill and respawn check
if (listeningWorkers === workers) {
cluster.removeListener('listening', listeningListenter);

process.nextTick(function () {
checkThrowHandling(newWorker);
});
}
});

var checkThrowHandling = function(killWorker) {

cluster.once('listening', function (newWorker) {

//Set autoRespawn
checks.autoRespawn = true;

//Check that the workerID is the same
checks.workerIDreuse = (killWorker.workerID === killWorker.workerID);

//Check that the uniqueID has been increesed
checks.uniqueIDincrease = (uniqueID > workerID && newWorker.uniqueID === uniqueID);

checkKillHandling(newWorker);
});

//Force worker to make throw an error
killWorker.send("please throw error");
};

var checkKillHandling = function (killWorker) {

var sometingsForked = false;

var forkCheck = function () {
sometingsForked = true;
};
cluster.once('fork', forkCheck);

//If after 1 sec no worker is forked, we can assume there was no autoRespawn
setTimeout(function () {

cluster.removeListener('fork', forkCheck);

if (sometingsForked === false) {
checks.suicideException = true;
}

process.nextTick(function () {
checkManualRespawn(killWorker);
});

}, 1000);

killWorker.kill();
};

var checkManualRespawn = function (killWorker) {

cluster.once('fork', function (newWorker) {
checks.manualRespawn = true;
checks.workerIDrespawnReuse = (killWorker.workerID === newWorker.workerID);
});

cluster.once('listening', function () {
process.exit(0);
});

cluster.autoFork();
};

//Start all workers
cluster.autoFork();

//Check all values
process.once('exit', function () {

assert.ok(checks.defaultWorkers, "There wasn't created the default number of worker");
assert.ok(checks.uniqueIDincrease, "The uniqueID did not increase correctly");
assert.ok(checks.workerIDincrease, "The workerID did not increase correctly");
assert.ok(checks.workerIDreuse, "The workerID was not resued when the worker respawn after an accidently death");
assert.ok(checks.workerIDrespawnReuse, "The workerID was not resued when a worker was manual respawned");
assert.ok(checks.onlineWorkers, "The onlineWorkers property did not return a correct value");
assert.ok(checks.startup, "The startup property was off by more that one second");
assert.ok(checks.autoRespawn, "There wasn't respawned a worker after an accidently death");
assert.ok(checks.manualRespawn, "The cluster module could not manually respawn worker");
assert.ok(checks.suicideException, "THe worker was respawn after a controlled suicide kill");
});

}
151 changes: 151 additions & 0 deletions test/simple/test-cluster-basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.


var common = require('../test/common');
var assert = require('assert');
var cluster = require('cluster');

function forEach(obj, fn) {
Object.keys(obj).forEach(function (name, index) {
fn(obj[name], name, index);
});
}

if (cluster.isWorker) {
var http = require('http');
http.Server(function () {

}).listen(common.PORT, "127.0.0.1");
}

else if (cluster.isMaster) {

var checks = {
cluster: {
events: {
fork: false,
online: false,
listening: false,
death: false
},
equal: {
fork: false,
online: false,
listening: false,
death: false
}
},

worker: {
events: {
online: false,
listening: false,
exit: false
},
equal: {
online: false,
listening: false,
exit: false
},
states: {
none: false,
online: false,
listening: false,
dead: false
}
}
};

var worker;
var stateNames = Object.keys(checks.worker.states);

//Check events, states, and emit arguments
forEach(checks.cluster.events, function (bool, name, index) {

//Listen on event
cluster.on(name, function (/*worker*/) {

//Set event
checks.cluster.events[name] = true;

//Check argument
checks.cluster.equal[name] = worker === arguments[0];

//Check state
checks.worker.states[stateNames[index]] = (stateNames[index] === worker.state);
});
});

//Kill worker when listening
cluster.on('listening', function () {
worker.kill();
});

//Kill process when worker is killed
cluster.on('death', function () {
process.exit(0);
});

//Create worker
worker = cluster.fork();
assert.ok(worker instanceof Object, "cluster.fork didn't return an object");

//Check event
forEach(checks.worker.events, function (bool, name, index) {
worker.on(name, function () {
//Set event
checks.worker.events[name] = true;

//Check argument
checks.worker.equal[name] = worker === arguments[0];
});
});

//Check all values
process.once('exit', function () {
//Check cluster events
forEach(checks.cluster.events, function (check, name) {
assert.ok(check, "The cluster event '" + name + "' on the cluster object didn't fire");
});

//Check cluster event arguments
forEach(checks.cluster.equal, function (check, name) {
assert.ok(check, "The cluster event '" + name + "' didn't emit with corrent argument");
});

//Check worker states
forEach(checks.worker.states, function (check, name) {
assert.ok(check, "The worker state '" + name + "' wasn't set to true");
});

//Check worker events
forEach(checks.worker.events, function (check, name) {
assert.ok(check, "The worker event '" + name + "' on the worker object didn't fire");
});

//Check worker event arguments
forEach(checks.worker.equal, function (check, name) {
assert.ok(check, "The worker event '" + name + "' didn't emit with corrent argument");
});
});

}
Loading

0 comments on commit 387b1f5

Please sign in to comment.