Skip to content

Commit

Permalink
Fixed bug regression caused by pervious commits. Also added tests tha…
Browse files Browse the repository at this point in the history
…t exposed regressions.
  • Loading branch information
xavi- committed Feb 21, 2011
1 parent c106fa7 commit 36fc6c2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/step.js
Expand Up @@ -26,7 +26,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() {
Expand All @@ -42,7 +42,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.
Expand All @@ -64,25 +64,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(); }
};
};
Expand All @@ -91,11 +91,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);
}
Expand All @@ -104,16 +105,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(); }
};
};
Expand Down
54 changes: 54 additions & 0 deletions test/groupTest.js
Expand Up @@ -45,4 +45,58 @@ Step(
fulfill('five');
assert.deepEqual(results, []);
}
);

// 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);
}
);
27 changes: 27 additions & 0 deletions test/parallelTest.js
Expand Up @@ -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 36fc6c2

Please sign in to comment.