Skip to content

Commit

Permalink
[api test] Update forever.Monitor.prototype.restart() to allow forc…
Browse files Browse the repository at this point in the history
…e restarting of processes in less than `.minUptime`
  • Loading branch information
indexzero committed Aug 13, 2011
1 parent f308f7a commit fdf15a0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
5 changes: 4 additions & 1 deletion lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Monitor.prototype.start = function (restart) {

function letChildDie() {
self.running = false;
self.forceStop = false;

//
// If had to write to an stdout file, close it
Expand All @@ -195,6 +196,7 @@ Monitor.prototype.start = function (restart) {
}

function restartChild() {
self.forceRestart = false;
process.nextTick(function () {
self.warn('Forever restarting script for ' + self.times + ' time');
self.start(true);
Expand All @@ -204,7 +206,7 @@ Monitor.prototype.start = function (restart) {
self.times++;

if (self.forceStop || (!self.forever && self.times >= self.max)
|| (spinning && typeof self.spinSleepTime !== 'number')) {
|| (spinning && typeof self.spinSleepTime !== 'number') && !self.forceRestart) {
letChildDie();
}
else if (spinning) {
Expand Down Expand Up @@ -309,6 +311,7 @@ Monitor.prototype.__defineGetter__('data', function () {
// Restarts the target script associated with this instance.
//
Monitor.prototype.restart = function () {
this.forceRestart = true;
return this.kill(false);
};

Expand Down
110 changes: 61 additions & 49 deletions test/forever-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,78 @@ var sys = require('sys'),
vows.describe('forever').addBatch({
"When using forever": {
"an instance of forever.Monitor with valid options": {
"should have correct properties set": function () {
var child = new (forever.Monitor)('any-file.js', {
max: 10,
silent: true,
options: []
});

topic: new (forever.Monitor)(path.join(__dirname, '..', 'examples', 'server.js'), {
max: 10,
silent: true,
options: ['-p', 8090]
}),
"should have correct properties set": function (child) {
assert.isArray(child.options);
assert.equal(child.max, 10);
assert.isTrue(child.silent);
assert.isFunction(child.start);
assert.isObject(child.data);
assert.isFunction(child.stop);
},
"running error-on-timer sample three times": helpers.assertTimes(
path.join(__dirname, '..', 'examples', 'error-on-timer.js'),
3,
{
minUptime: 200,
silent: true,
outFile: 'test/stdout.log',
errFile: 'test/stderr.log',
options: []
}
),
"running error-on-timer sample once": helpers.assertTimes(
path.join(__dirname, '..', 'examples', 'error-on-timer.js'),
1,
{
minUptime: 200,
silent: true,
outFile: 'test/stdout.log',
errFile: 'test/stderr.log',
options: []
}
),
"non-node usage with a perl one-liner": {
topic: function () {
var child = forever.start([ 'perl', '-le', 'print "moo"' ], {
max: 1,
silent: true,
"calling the restart() method in less than `minUptime`": {
topic: function (child) {
var that = this;
child.once('start', function () {
child.once('restart', that.callback.bind(this, null));
child.restart();
});
child.on('stdout', this.callback.bind({}, null));
child.start();
},
"should get back moo": function (err, buf) {
assert.equal(buf.toString(), 'moo\n');
"should restart the child process": function (_, _, data) {
assert.isObject(data);
}
}
},
"running error-on-timer sample three times": helpers.assertTimes(
path.join(__dirname, '..', 'examples', 'error-on-timer.js'),
3,
{
minUptime: 200,
silent: true,
outFile: 'test/stdout.log',
errFile: 'test/stderr.log',
options: []
}
),
"running error-on-timer sample once": helpers.assertTimes(
path.join(__dirname, '..', 'examples', 'error-on-timer.js'),
1,
{
minUptime: 200,
silent: true,
outFile: 'test/stdout.log',
errFile: 'test/stderr.log',
options: []
}
),
"non-node usage with a perl one-liner": {
topic: function () {
var child = forever.start([ 'perl', '-le', 'print "moo"' ], {
max: 1,
silent: true,
});
child.on('stdout', this.callback.bind({}, null));
},
"attempting to start a script that doesn't exist": {
topic: function () {
var child = forever.start('invalid-path.js', {
max: 1,
silent: true
});
child.on('error', this.callback.bind({}, null));
},
"should throw an error about the invalid file": function (err) {
assert.isNotNull(err);
assert.isTrue(err.message.indexOf('does not exist') !== -1);
}
"should get back moo": function (err, buf) {
assert.equal(buf.toString(), 'moo\n');
}
},
"attempting to start a script that doesn't exist": {
topic: function () {
var child = forever.start('invalid-path.js', {
max: 1,
silent: true
});
child.on('error', this.callback.bind({}, null));
},
"should throw an error about the invalid file": function (err) {
assert.isNotNull(err);
assert.isTrue(err.message.indexOf('does not exist') !== -1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/spawn-options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var sys = require('sys'),
forever = require('../lib/forever'),
helpers = require('./helpers');

vows.describe('forever').addBatch({
vows.describe('forever/spawn-options').addBatch({
"When using forever": {
"an instance of forever.Monitor with valid options": {
"passing environment variables to env-vars.js": {
Expand Down

0 comments on commit fdf15a0

Please sign in to comment.