Permalink
Browse files

Fixed bug regression caused by pervious commits. Also added tests tha…

…t exposed regressions.
  • Loading branch information...
1 parent 2f0a49d commit 2010cb81ab80c71ccbc04a0e4d59fee9b9f3e43e @xavi- xavi- committed Feb 20, 2011
Showing with 97 additions and 15 deletions.
  1. +13 −12 lib/step.js
  2. +57 −3 test/groupTest.js
  3. +27 −0 test/parallelTest.js
View
@@ -24,7 +24,7 @@ SOFTWARE.
// modified to fit my taste and the node.JS error handling system.
function Step() {
var steps = Array.prototype.slice.call(arguments),
- counter, results, lock;
+ pending, counter, results, lock;
// Define the main callback that's given as `this` to the steps.
function next() {
@@ -40,7 +40,7 @@ function Step() {
// Get the next step to execute
var fn = steps.shift();
- counter = 0;
+ counter = pending = 0;
results = [];
// Run the step in a try..catch block so exceptions don't get out of hand.
@@ -62,25 +62,25 @@ function Step() {
// Add a special callback generator `this.parallel()` that groups stuff.
next.parallel = function () {
- var i = counter;
- counter++;
+ var index = 1 + counter++;
+ pending++;
function check() {
- if (counter === 0) {
+ if (pending === 0) {
// When they're all done, call the callback
next.apply(null, results);
}
}
process.nextTick(check); // Ensures that check is called at least once
return function () {
- counter--;
+ pending--;
// Compress the error from any result to the first argument
if (arguments[0]) {
results[0] = arguments[0];
}
// Send the other results as arguments
- results[i + 1] = arguments[1];
+ results[index] = arguments[1];
if (!lock) { check(); }
};
};
@@ -89,11 +89,12 @@ function Step() {
next.group = function () {
var localCallback = next.parallel();
var counter = 0;
+ var pending = 0;
var result = [];
var error = undefined;
function check() {
- if (counter === 0) {
+ if (pending === 0) {
// When group is done, call the callback
localCallback(error, result);
}
@@ -102,16 +103,16 @@ function Step() {
// Generates a callback for the group
return function () {
- var i = counter;
- counter++;
+ var index = counter++;
+ pending++;
return function () {
- counter--;
+ pending--;
// Compress the error from any result to the first argument
if (arguments[0]) {
error = arguments[0];
}
// Send the other results as arguments
- result[i] = arguments[1];
+ result[index] = arguments[1];
if (!lock) { check(); }
};
};
View
@@ -33,12 +33,66 @@ Step(
);
// Test to make sure groups of zero size terminate
-expect('group of zero terminated');
+expect('test2: group of zero terminated');
Step(
function makeGroup() {
this.group();
},
function terminate() {
- fulfill('group of zero terminated');
+ fulfill('test2: group of zero terminated');
}
-)
+);
+
+// Test lock functionality with N sized groups
+expect("test3: 1");
+expect("test3: 1,2,3");
+expect("test3: 2");
+Step(
+ function() {
+ return 1;
+ },
+ function makeGroup(err, num) {
+ if(err) throw err;
+ fulfill("test3: " + num);
+ var group = this.group();
+
+ setTimeout((function(callback) { return function() { callback(null, 1); } })(group()), 100);
+ group()(null, 2);
+ setTimeout((function(callback) { return function() { callback(null, 3); } })(group()), 0);
+ },
+ function groupResults(err, results) {
+ if(err) throw err;
+ fulfill("test3: " + results);
+ return 2
+ },
+ function terminate(err, num) {
+ if(err) throw err;
+ fulfill("test3: " + num);
+ }
+);
+
+// Test lock functionality with zero sized groups
+expect("test4: 1");
+expect("test4: empty array");
+expect("test4: group of zero terminated");
+expect("test4: 2");
+Step(
+ function() {
+ return 1;
+ },
+ function makeGroup(err, num) {
+ if(err) throw err;
+ fulfill("test4: " + num);
+ this.group();
+ },
+ function groupResults(err, results) {
+ if(err) throw err;
+ if(results.length === 0) { fulfill("test4: empty array"); }
+ fulfill('test4: group of zero terminated');
+ return 2
+ },
+ function terminate(err, num) {
+ if(err) throw err;
+ fulfill("test4: " + num);
+ }
+);
View
@@ -20,3 +20,30 @@ Step(
assert.equal(etcText, users, "Users should come second");
}
);
+
+// Test lock functionality with N sized groups
+expect("test2: 1");
+expect("test2: 1,2,3");
+expect("test2: 2");
+Step(
+ function() {
+ return 1;
+ },
+ function makeParallelCalls(err, num) {
+ if(err) throw err;
+ fulfill("test2: " + num);
+
+ setTimeout((function(callback) { return function() { callback(null, 1); } })(this.parallel()), 100);
+ this.parallel()(null, 2);
+ setTimeout((function(callback) { return function() { callback(null, 3); } })(this.parallel()), 0);
+ },
+ function parallelResults(err, one, two, three) {
+ if(err) throw err;
+ fulfill("test2: " + [one, two, three]);
+ return 2
+ },
+ function terminate(err, num) {
+ if(err) throw err;
+ fulfill("test2: " + num);
+ }
+)

0 comments on commit 2010cb8

Please sign in to comment.