summaryrefslogtreecommitdiff
path: root/node_modules/forever/test
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/forever/test')
-rwxr-xr-xnode_modules/forever/test/cli-test51
-rw-r--r--node_modules/forever/test/core/tail-stopall-test.js47
-rw-r--r--node_modules/forever/test/fixtures/log-on-interval.js3
-rw-r--r--node_modules/forever/test/fixtures/server.js16
-rw-r--r--node_modules/forever/test/fixtures/start-daemon.js16
-rw-r--r--node_modules/forever/test/helpers/macros.js93
-rw-r--r--node_modules/forever/test/helpers/mocks/child-process.js12
-rw-r--r--node_modules/forever/test/helpers/mocks/monitor.js25
-rw-r--r--node_modules/forever/test/helpers/mocks/stream.js19
-rw-r--r--node_modules/forever/test/service/simple-test.js25
-rw-r--r--node_modules/forever/test/worker/multiple-workers-test.js98
-rw-r--r--node_modules/forever/test/worker/simple-test.js73
12 files changed, 478 insertions, 0 deletions
diff --git a/node_modules/forever/test/cli-test b/node_modules/forever/test/cli-test
new file mode 100755
index 0000000..13cb816
--- /dev/null
+++ b/node_modules/forever/test/cli-test
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+
+#
+# cli-test: Tests for forever CLI
+#
+# (C) 2012 Nodejitsu Inc.
+# MIT LICENSE
+#
+
+# Yes, we have tests in bash. How mad science is that?
+
+alias forever=bin/forever
+script="test/fixtures/log-on-interval.js"
+
+function fail {
+ echo "\033[31m ✘ $1\033[0m"
+ exit 1
+}
+
+function success {
+ echo "\033[32m ✔ $1\033[0m"
+}
+
+function spec {
+ [ $? -eq 0 ] || fail "$1"
+ success "$1"
+}
+
+echo "\033[1mRunning tests:\033[0m"
+
+# First kill all processes and remove forever directory to ensure clean
+# environment
+forever stopall
+rm -rf ~/.forever
+
+# Spawn some process
+forever start "$script"
+
+# Assert that forever actually spawned a process and that it's in `forever list`
+sleep 1 # it takes some time until process appears in `forever list`
+forever list | grep "$script"
+spec "\`forever list\` should contain spawned process"
+
+# `forever stop` should output process it stopped...
+forever stop 0 | grep "$script"
+spec "\`forever stop 0\` should contain stopped process"
+
+# ... and actually stop it
+forever list | grep -v "$script"
+spec "\`forever stop 0\` should actually stop the process"
+
diff --git a/node_modules/forever/test/core/tail-stopall-test.js b/node_modules/forever/test/core/tail-stopall-test.js
new file mode 100644
index 0000000..973d0b9
--- /dev/null
+++ b/node_modules/forever/test/core/tail-stopall-test.js
@@ -0,0 +1,47 @@
+/*
+ * tail-stopall-test.js: Tests for forever.tail() and forever.stopAll()
+ *
+ * (C) 2010 Nodejitsu Inc.
+ * MIT LICENCE
+ *
+ */
+
+var assert = require('assert'),
+ path = require('path'),
+ spawn = require('child_process').spawn,
+ vows = require('vows'),
+ forever = require('../../lib/forever');
+
+vows.describe('forever/core/tail').addBatch({
+ "When using forever": {
+ "the tail() method": {
+ topic: function () {
+ var that = this;
+
+ that.child = spawn('node', [path.join(__dirname, '..', 'fixtures', 'start-daemon.js')]);
+ setTimeout(function () {
+ forever.tail(0, { length: 1 }, that.callback);
+ }, 2000);
+ },
+ "should respond with logs for the script": function (err, procs) {
+ assert.isNull(err);
+ assert.equal(typeof procs, 'object');
+ assert.ok(procs.file);
+ assert.ok(procs.pid);
+ assert.ok(procs.line);
+ }
+ }
+ }
+}).addBatch({
+ "When the tests are over": {
+ "stop all forever processes": {
+ topic: function () {
+ forever.stopAll().on('stopAll', this.callback.bind(null, null));
+ },
+ "should stop the correct number of procs": function (err, procs) {
+ assert.isArray(procs);
+ assert.lengthOf(procs, 1);
+ }
+ }
+ }
+}).export(module);
diff --git a/node_modules/forever/test/fixtures/log-on-interval.js b/node_modules/forever/test/fixtures/log-on-interval.js
new file mode 100644
index 0000000..6b27652
--- /dev/null
+++ b/node_modules/forever/test/fixtures/log-on-interval.js
@@ -0,0 +1,3 @@
+setInterval(function () {
+ console.log('Logging at ' + Date.now());
+}, 100); \ No newline at end of file
diff --git a/node_modules/forever/test/fixtures/server.js b/node_modules/forever/test/fixtures/server.js
new file mode 100644
index 0000000..a386de2
--- /dev/null
+++ b/node_modules/forever/test/fixtures/server.js
@@ -0,0 +1,16 @@
+var util = require('util'),
+ http = require('http'),
+ argv = require('optimist').argv;
+
+var port = argv.p || argv.port || 80;
+
+http.createServer(function (req, res) {
+ console.log(req.method + ' request: ' + req.url);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write('hello, i know nodejitsu.');
+ res.end();
+}).listen(port);
+
+/* server started */
+util.puts('> hello world running on port ' + port);
+
diff --git a/node_modules/forever/test/fixtures/start-daemon.js b/node_modules/forever/test/fixtures/start-daemon.js
new file mode 100644
index 0000000..9bf8eb6
--- /dev/null
+++ b/node_modules/forever/test/fixtures/start-daemon.js
@@ -0,0 +1,16 @@
+/*
+ * start-daemon.js: Simple test fixture for spawning log-on-interval.js as a daemon
+ *
+ * (C) 2010 Nodejitsu Inc.
+ * MIT LICENCE
+ *
+ */
+
+var path = require('path'),
+ forever = require('../../lib/forever');
+
+var monitor = forever.startDaemon(path.join(__dirname, 'log-on-interval.js'));
+
+monitor.on('start', function () {
+ forever.startServer(monitor);
+});
diff --git a/node_modules/forever/test/helpers/macros.js b/node_modules/forever/test/helpers/macros.js
new file mode 100644
index 0000000..47471f4
--- /dev/null
+++ b/node_modules/forever/test/helpers/macros.js
@@ -0,0 +1,93 @@
+/*
+ * macros.js: Test macros for the forever module
+ *
+ * (C) 2010 Nodejitsu Inc.
+ * MIT LICENCE
+ *
+ */
+
+var assert = require('assert'),
+ path = require('path'),
+ spawn = require('child_process').spawn,
+ nssocket = require('nssocket'),
+ forever = require('../../lib/forever'),
+ Worker = require('../../lib/forever/worker').Worker;
+
+var macros = exports;
+
+macros.assertTimes = function (script, times, options) {
+ options.max = times;
+
+ return {
+ topic: function () {
+ var child = new (forever.Monitor)(script, options);
+ child.on('exit', this.callback.bind({}, null));
+ child.start();
+ },
+ "should emit 'exit' when completed": function (err, child) {
+ assert.equal(child.times, times);
+ }
+ }
+};
+
+macros.spawn = function (args, options) {
+ options.topic = function () {
+ var self = this;
+
+ args = [path.join(__dirname, '..', 'bin', 'forever')].concat(args);
+
+ var child = spawn(process.argv[0], args),
+ stdout = '',
+ stderr = '';
+
+ child.stdout.on('data', function (data) {
+ stdout += data;
+ });
+ child.stderr.on('data', function (data) {
+ stderr += data;
+ });
+ child.once('exit', function (exitCode) {
+ //
+ // Remark: We wait 200 ms because of forever boot up time (master
+ // doesn't wait for slave to start up after it's forked, it just quits)
+ //
+ setTimeout(function () {
+ self.callback(null, exitCode, stdout, stderr);
+ }, 200);
+ });
+ };
+ return options;
+};
+
+macros.list = function (options) {
+ options.topic = function () {
+ forever.list(false, this.callback)
+ };
+ return options;
+};
+
+macros.assertStartsWith = function (string, substring) {
+ assert.equal(string.slice(0, substring.length), substring);
+};
+
+macros.assertList = function (list) {
+ assert.isNotNull(list);
+ assert.lengthOf(list, 1);
+};
+
+macros.assertWorkerConnected = function (workerOptions, batch) {
+ return {
+ topic: function () {
+ var self = this,
+ reader = new nssocket.NsSocket(),
+ worker = new Worker(workerOptions);
+
+ worker.start(function (err, sock) {
+ reader.connect(sock, function () {
+ self.callback(null, reader, worker, workerOptions);
+ });
+ });
+ },
+ 'worker should connect': batch
+ };
+};
diff --git a/node_modules/forever/test/helpers/mocks/child-process.js b/node_modules/forever/test/helpers/mocks/child-process.js
new file mode 100644
index 0000000..5b96147
--- /dev/null
+++ b/node_modules/forever/test/helpers/mocks/child-process.js
@@ -0,0 +1,12 @@
+var util = require('util'),
+ EventEmitter2 = require('eventemitter2').EventEmitter2,
+ StreamMock = require('./stream').StreamMock;
+
+var ChildProcessMock = exports.ChildProcessMock = function () {
+ EventEmitter2.call(this);
+
+ this.stdout = new StreamMock();
+ this.stderr = new StreamMock();
+};
+util.inherits(ChildProcessMock, EventEmitter2);
+
diff --git a/node_modules/forever/test/helpers/mocks/monitor.js b/node_modules/forever/test/helpers/mocks/monitor.js
new file mode 100644
index 0000000..411780d
--- /dev/null
+++ b/node_modules/forever/test/helpers/mocks/monitor.js
@@ -0,0 +1,25 @@
+var util = require('util'),
+ broadway = require('broadway'),
+ ChildProcessMock = require('./child-process').ChildProcessMock;
+
+var MonitorMock = exports.MonitorMock = function (options) {
+ broadway.App.call(this, options);
+
+ this.child = new ChildProcessMock();
+ this.running = false;
+};
+util.inherits(MonitorMock, broadway.App);
+
+MonitorMock.prototype.__defineGetter__('data', function () {
+ return {
+ uid: '_uid',
+ command: 'node'
+ }
+});
+
+MonitorMock.prototype.kill = MonitorMock.prototype.stop = function (forceStop) {
+ this.running = false;
+
+ this.emit('stop');
+};
+
diff --git a/node_modules/forever/test/helpers/mocks/stream.js b/node_modules/forever/test/helpers/mocks/stream.js
new file mode 100644
index 0000000..17b04e2
--- /dev/null
+++ b/node_modules/forever/test/helpers/mocks/stream.js
@@ -0,0 +1,19 @@
+var util = require('util'),
+ EventEmitter2 = require('eventemitter2').EventEmitter2;
+
+var StreamMock = exports.StreamMock = function () {
+ EventEmitter2.call(this);
+
+ this.contents = [];
+ this.closed = false;
+};
+util.inherits(StreamMock, EventEmitter2);
+
+StreamMock.prototype.write = function (data) {
+ this.contents.push(data);
+};
+
+StreamMock.prototype.close = StreamMock.prototype.end = function () {
+ this.closed = true;
+};
+
diff --git a/node_modules/forever/test/service/simple-test.js b/node_modules/forever/test/service/simple-test.js
new file mode 100644
index 0000000..5f58fad
--- /dev/null
+++ b/node_modules/forever/test/service/simple-test.js
@@ -0,0 +1,25 @@
+/*
+ * service-test.js: Tests for the forever.service module
+ *
+ * (C) 2010 Nodejitsu Inc.
+ * MIT LICENCE
+ *
+ */
+
+var assert = require('assert'),
+ path = require('path'),
+ vows = require('vows'),
+ forever = require('../../lib/forever');
+
+vows.describe('forever/service/simple').addBatch({
+ "When using forever": {
+ "the service module": {
+ "should have the correct exports": function () {
+ assert.isObject(forever.service);
+ assert.isFunction(forever.service.Service);
+ assert.isObject(forever.service.adapters);
+ assert.isFunction(forever.service.adapters.initd);
+ }
+ }
+ }
+}).export(module);
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);
+