Permalink
Browse files

Added tests

  • Loading branch information...
1 parent abdd2b9 commit d211110c14bf67ee665d39266661a1153ad4d645 @alexkwolfe committed Aug 11, 2010
Showing with 190 additions and 7 deletions.
  1. +61 −0 README
  2. +1 −7 lib/gang-bang.js
  3. +26 −0 test/run_tests.js
  4. +50 −0 test/unit/chain_test.js
  5. +52 −0 test/unit/group_test.js
View
61 README
@@ -0,0 +1,61 @@
+# Gang Bang
+
+A flow control library for node.js for executing multiple functions as a group or in a chain, calling back when all functions have finished.
+
+## Installation
+
+ npm install gang-bang
+
+## Usage
+
+ var gang = require('gang-bang'),
+ puts = require('sys').puts;
+
+ // the array of functions to execute. each calls the done function
+ // upon completion, passing back relevant information.
+ var functions = [
+ function(done) { done('red'); },
+ function(done) { done('green'); },
+ function(done) { done('blue'); }
+ ];
+
+ // execute functions concurrently, and callback when all functions have been called
+ gang.group(functions, function(colors) {
+ puts("All colors were collected: " + colors);
+ });
+
+ // execute each one after the other, and callback when all functions have been called
+ gang.chain(functions, function(colors) {
+ puts("All colors were collected: " + colors);
+ });
+
+## Error handling
+
+If an error occurs in a function, pass the error to the `done` method. The error will be included
+among the values passed to the callback function.
+
+When an error occurs during chained execution, the chain stops and the values collected (including
+the error) are returned to the callback function.
+
+Check for errors in the values passed to the callback function and behave accordingly.
+
+ var gang = require('gang-bang'),
+ puts = require('sys').puts;
+
+ // the array of functions to execute. each calls the done function
+ // upon completion, passing back relevant information.
+ var functions = [
+ function(done) { done('red'); },
+ function(done) { done(new Error('something went wrong')); },
+ function(done) { done('blue'); }
+ ];
+
+ // execute functions concurrently, and callback when all functions have been called
+ gang.group(functions, function(colors) {
+ puts("Two colors and one error were collected: " + colors);
+ });
+
+ // execute each one after the other, and callback when the error occurrs
+ gang.chain(functions, function(colors) {
+ puts("One color and one error were collected: " + colors);
+ });
View
@@ -12,11 +12,8 @@ exports.group = function (fxns, cb) {
var items_left_to_execute = fxns.length;
var results = [];
- var errors = [];
var callGroupFunction = function(fxn) {
var done = function(result) {
- if (result instanceof Error)
- return cb(result);
results.push(result);
items_left_to_execute--;
if (!items_left_to_execute)
@@ -41,14 +38,11 @@ exports.chain = function (fxns, callback) {
var results = [];
var callNext = function() {
var done = function(result) {
- if (result instanceof Error)
- return callback(result);
-
pos++;
if (result)
results.push(result);
- if (fxns.length > pos) {
+ if (!(result instanceof Error) && fxns.length > pos) {
callNext();
} else {
callback(results);
View
@@ -0,0 +1,26 @@
+#!/usr/local/bin/node
+
+require.paths.push(__dirname);
+require.paths.push(__dirname + '/lib');
+require('nodeunit');
+
+try {
+ var testrunner = require('nodeunit').testrunner;
+}
+catch(e) {
+ var sys = require('sys');
+ sys.puts("Cannot find nodeunit module.");
+ sys.puts("You can download submodules for this project by doing:");
+ sys.puts("");
+ sys.puts(" git submodule init");
+ sys.puts(" git submodule update");
+ sys.puts("");
+ process.exit();
+}
+
+process.chdir(__dirname);
+testrunner.run(
+ [
+ 'unit'
+ ]
+);
View
@@ -0,0 +1,50 @@
+var gang = require('../../lib/gang-bang');
+
+exports.testAllFunctionsCalled = function(assert) {
+ assert.expect(1);
+
+ var calls = 0;
+ var functions = [
+ function(done) { calls++; done(); },
+ function(done) { calls++; done(); },
+ function(done) { calls++; done(); }
+ ];
+
+ gang.chain(functions, function(colors) {
+ assert.equals(3, calls);
+ assert.done();
+ });
+}
+
+exports.testResults = function(assert) {
+ assert.expect(1);
+
+ var functions = [
+ function(done) { done('red'); },
+ function(done) { done('green'); },
+ function(done) { done('blue'); }
+ ];
+
+ gang.chain(functions, function(colors) {
+ assert.same(['red', 'green', 'blue'], colors);
+ assert.done();
+ });
+};
+
+exports.testStopsOnError = function(assert) {
+ assert.expect(4);
+
+ var functions = [
+ function(done) { done('red'); },
+ function(done) { done(new Error('green')); },
+ function(done) { done('blue'); }
+ ];
+
+ gang.chain(functions, function(colors) {
+ assert.equals(2, colors.length);
+ assert.equals('red', colors[0]);
+ assert.ok(colors[1] instanceof Error);
+ assert.equals('green', colors[1].message);
+ assert.done();
+ });
+};
View
@@ -0,0 +1,52 @@
+var gang = require('../../lib/gang-bang');
+
+exports.testAllFunctionsCalled = function(assert) {
+ assert.expect(1);
+
+ var calls = 0;
+ var functions = [
+ function(done) { calls++; done(); },
+ function(done) { calls++; done(); },
+ function(done) { calls++; done(); }
+ ];
+
+ gang.group(functions, function(colors) {
+ assert.equals(3, calls);
+ assert.done();
+ });
+}
+
+exports.testResults = function(assert) {
+ assert.expect(1);
+
+ // order the results by setting a timeout. this "proves" concurrency.
+ var functions = [
+ function(done) { setTimeout(function() { done('red'); }, 300); },
+ function(done) { setTimeout(function() { done('green'); }, 100); },
+ function(done) { setTimeout(function() { done('blue'); }, 200); },
+ ];
+
+ gang.group(functions, function(colors) {
+ assert.same(['green', 'blue', 'red'], colors);
+ assert.done();
+ });
+};
+
+exports.testDoesNotStopOnError = function(assert) {
+ assert.expect(5);
+
+ var functions = [
+ function(done) { setTimeout(function() { done('red'); }, 100); },
+ function(done) { setTimeout(function() { done(new Error('green')); }, 200); },
+ function(done) { setTimeout(function() { done('blue'); }, 300); },
+ ];
+
+ gang.group(functions, function(colors) {
+ assert.equals(3, colors.length);
+ assert.equals('red', colors[0]);
+ assert.ok(colors[1] instanceof Error);
+ assert.equals('green', colors[1].message);
+ assert.equals('blue', colors[2]);
+ assert.done();
+ });
+};

0 comments on commit d211110

Please sign in to comment.