diff options
Diffstat (limited to 'node_modules/forever/test/worker')
| -rw-r--r-- | node_modules/forever/test/worker/multiple-workers-test.js | 98 | ||||
| -rw-r--r-- | node_modules/forever/test/worker/simple-test.js | 73 |
2 files changed, 171 insertions, 0 deletions
diff --git a/node_modules/forever/test/worker/multiple-workers-test.js b/node_modules/forever/test/worker/multiple-workers-test.js new file mode 100644 index 0000000..dca1685 --- /dev/null +++ b/node_modules/forever/test/worker/multiple-workers-test.js @@ -0,0 +1,98 @@ +/* + * multiple-workers-test.js: Tests for spawning multiple workers with forever + * + * (C) 2010 Nodejitsu Inc. + * MIT LICENCE + * + */ + +var assert = require('assert'), + net = require('net'), + path = require('path'), + request = require('request'), + vows = require('vows'), + forever = require('../../lib/forever'); + +var children = [], + pids; + +// +// Helper function test requests against children. +// +function assertRunning(port, i) { + return { + topic: function () { + request('http://127.0.0.1:' + port, this.callback); + }, + "should respond with `i know nodejitsu`": function (err, res, body) { + assert.isNull(err); + assert.equal(res.statusCode, 200); + assert.equal(body, 'hello, i know nodejitsu.'); + }, + "stop the child process": function () { + children[i].stop(); + } + } +} + +vows.describe('forever/workers/multiple').addBatch({ + "When using forever": { + "and spawning two processes using the same script": { + topic: function () { + var that = this, + script = path.join(__dirname, '..', 'fixtures', 'server.js'); + + children[0] = new (forever.Monitor)(script, { + silent: true, + maxRestart: 1, + options: [ "--port=8080"] + }); + + children[1] = new (forever.Monitor)(script, { + silent: true, + maxRestart: 1, + options: [ "--port=8081"] + }); + + children[0].on('start', function () { + children[1].on('start', function () { + pids = children.map(function (child) { + return child.child.pid; + }); + + setTimeout(function () { + forever.startServer(children[0], children[1], that.callback); + }, 1000); + }); + + children[1].start(); + }); + + children[0].start(); + }, + "should respond with no error": function (err, workers) { + assert.lengthOf(workers, 2); + assert.equal(workers[0].monitor, children[0]); + assert.equal(workers[1].monitor, children[1]); + workers.forEach(function (worker) { + assert.instanceOf(worker, forever.Worker); + }); + }, + "requests against the first child": assertRunning(8080, 0), + "requests against the second child": assertRunning(8081, 1) + // + // TODO: We should cleanup these processes. + // + } + }, +}).addBatch({ + "Once the stop attempt has been made": { + topic: function () { + setTimeout(this.callback, 200); + }, + "the processes should be dead": function () { + assert.isFalse(forever.checkProcess(pids[0])); + assert.isFalse(forever.checkProcess(pids[1])); + } + } +}).export(module); diff --git a/node_modules/forever/test/worker/simple-test.js b/node_modules/forever/test/worker/simple-test.js new file mode 100644 index 0000000..26c4fb1 --- /dev/null +++ b/node_modules/forever/test/worker/simple-test.js @@ -0,0 +1,73 @@ +var path = require('path'), + assert = require('assert'), + vows = require('vows'), + nssocket = require('nssocket'), + macros = require('../helpers/macros'), + MonitorMock = require('../helpers/mocks/monitor').MonitorMock; + +var SOCKET_PATH = path.join(__dirname, '..', 'fixtures'); + +vows.describe('forever/worker/simple').addBatch({ + 'When using forever worker': { + 'and starting it and pinging it': macros.assertWorkerConnected({ + monitor: new MonitorMock(), + sockPath: SOCKET_PATH + }, { + 'and respond to pings': { + topic: function (reader) { + reader.send(['ping']); + reader.data(['pong'], this.callback); + }, + 'with `pong`': function () {} + }, + 'and when queried for data': { + topic: function (reader, _, options) { + var self = this; + + reader.send(['data']); + reader.data(['data'], function (data) { + self.callback(null, { data: data, monitor: options.monitor }); + }); + }, + 'it should respond with data': function (obj) { + assert.isObject(obj.data); + assert.deepEqual(obj.data, obj.monitor.data); + } + }, + 'and when asked to kill the process': { + topic: function (reader, _, options) { + var self = this; + + options.monitor.running = true; + reader.send(['stop']); + reader.data(['stop', 'ok'], function () { + self.callback(null, options.monitor); + }); + }, + 'it should kill the process': function (monitor) { + assert.isFalse(monitor.running); + } + }, + 'and when quickly sending data and disconnecting': { + topic: function(reader) { + var self = this; + + // Need to connect second reader, otherwise it breaks the other + // tests as the reader is shared with them. + var reader2 = new nssocket.NsSocket(); + reader2.connect(reader.host, function() { + reader2.send(['data']); + reader2.destroy(); + + setTimeout(self.callback, 100); + }); + }, + 'it should not crash the worker': function(worker) { + // no asserition, everything is good if the test does not cause + // a worker crash. + } + } + }) + } +}).export(module); + |
