Skip to content

Commit

Permalink
[test fix] Since forever.kill is async, use async.forEach. Update t…
Browse files Browse the repository at this point in the history
…est/cli-test.js to rimraf ~/.forever temporarily
  • Loading branch information
indexzero committed Nov 23, 2011
1 parent 1a04002 commit 60a576a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
45 changes: 22 additions & 23 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ function getAllProcesses(callback) {
});

socket.on('close', function () {
var monitors = JSON.parse(data);
var monitors;
try {
monitors = JSON.parse(data);
}
catch (ex) {
return next();
}
results.push.apply(results, monitors.monitors);
next();
});
Expand Down Expand Up @@ -414,20 +420,21 @@ forever.startServer = function () {
//
forever.stop = function (target, format) {
var emitter = new events.EventEmitter(),
results = [];
results = [],
pids;

getAllProcesses(function (processes) {
var procs = forever.findByIndex(target, processes)
|| forever.findByScript(target, processes);

if (procs && procs.length > 0) {
procs.forEach(function (proc) {
[proc.foreverPid, proc.pid].forEach(function (pid) {
forever.kill(pid, true);
});
});
pids = procs.reduce(function (agg, proc) {
return agg.concat(proc.foreverPid, proc.pid);
}, []);

process.nextTick(function () {
async.forEach(pids, function (pid, next) {
forever.kill(pid, true, next);
}, function () {
emitter.emit('stop', forever.format(format, procs));
});
}
Expand Down Expand Up @@ -538,30 +545,22 @@ forever.stopAll = function (format) {
var emitter = new events.EventEmitter();

getAllProcesses(function (processes) {
var pids = getAllPids(processes),
fPids,
cPids;
var pids = getAllPids(processes);

if (format) {
processes = forever.format(format, processes);
}

if (pids && processes) {
fPids = pids.map(function (pid) {
return pid.foreverPid;
});
pids = pids.reduce(function (agg, proc) {
return agg.concat(proc.foreverPid, proc.pid);
}, []);

cPids = pids.map(function (pid) {
return pid.pid;
});

fPids.concat(cPids).forEach(function (pid) {
async.forEach(pids, function (pid, next) {
if (pid !== process.pid) {
forever.kill(pid, true);
forever.kill(pid, true, next);
}
});

process.nextTick(function () {
}, function () {
emitter.emit('stopAll', processes);
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/forever/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ cli.startCLI = function () {
if (app.config.get('help')) {
return util.puts(help.join('\n'));
}

app.start();
});
};
Expand Down
24 changes: 23 additions & 1 deletion test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ var fs = require('fs'),
assert = require('assert'),
vows = require('vows'),
helpers = require('./helpers'),
rimraf = require('utile').rimraf,
forever = require('../lib/forever');

var script = path.join(__dirname, '..', 'examples', 'log-on-interval.js');

vows.describe('forever/cli').addBatch({
'When using forever CLI': {
<<<<<<< HEAD
'and starting script using `forever start`': helpers.spawn(['start', script], {
'`forever.list` result': helpers.list({
'should contain spawned process': function (list) {
Expand All @@ -31,10 +33,30 @@ vows.describe('forever/cli').addBatch({
'should not contain previous process': function (list) {
assert.isNull(list);
}
=======
"when ~/.forever/sock is empty": {
topic: function () {
rimraf(path.join(process.env.HOME, '.forever', 'sock'), this.callback);
},
'and starting script using `forever start`': helpers.spawn(['start', script], {
'`forever.list` result': helpers.list({
'should contain spawned process': function (list) {
helpers.assertList(list);
assert.equal(list[0].command, 'node');
assert.equal(fs.realpathSync(list[0].file), fs.realpathSync(script));
helpers.assertStartsWith(list[0].logFile, forever.config.get('root'));
},
'and stopping it using `forever stop`': helpers.spawn(['stop', script], {
'`forever.list` result': helpers.list({
'should not contain previous process': function (list) {
assert.isNull(list);
}
})
>>>>>>> b622bf1... [test fix] Since forever.kill is async, use `async.forEach`. Update test/cli-test.js to rimraf ~/.forever temporarily
})
})
})
})
}
}
}).export(module);

0 comments on commit 60a576a

Please sign in to comment.