Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix bug with parallel.

The bug:
When a step called parallel() N times, and those callbacks were called
syncronously immediately, step would execute the next N steps immediately,
rather than just the following step.
  • Loading branch information...
commit 5c56c26dbb844878ebaf4b3610fc46b301e95c4d 1 parent b67f971
@sethml sethml authored
Showing with 99 additions and 7 deletions.
  1. +7 −5 lib/step.js
  2. +33 −1 test/groupTest.js
  3. +59 −1 test/parallelTest.js
View
12 lib/step.js
@@ -30,6 +30,7 @@ function Step() {
// Define the main callback that's given as `this` to the steps.
function next() {
+ counter = pending = 0;
// Check if there are no steps left
if (steps.length === 0) {
@@ -42,7 +43,6 @@ function Step() {
// Get the next step to execute
var fn = steps.shift();
- counter = pending = 0;
results = [];
// Run the step in a try..catch block so exceptions don't get out of hand.
@@ -54,9 +54,12 @@ function Step() {
next(e);
}
-
- // If a syncronous return is used, pass it to the callback
- if (result !== undefined) {
+ if (counter > 0 && pending == 0) {
+ // If parallel() was called, and all parallel branches executed
+ // syncronously, go on to the next step now.
+ next.apply(null, results);
+ } else if (result !== undefined) {
+ // If a syncronous return is used, pass it to the callback
next(undefined, result);
}
lock = false;
@@ -73,7 +76,6 @@ function Step() {
next.apply(null, results);
}
}
- process.nextTick(check); // Ensures that check is called at least once
return function () {
pending--;
View
34 test/groupTest.js
@@ -99,4 +99,36 @@ Step(
if(err) throw err;
fulfill("test4: " + num);
}
-);
+);
+
+
+// Test lock functionality with groups which return immediately
+expect("test5: 1,2");
+expect("test5 t1: 666");
+expect("test5 t2: 333");
+setTimeout(function() {
+Step(
+ function parallelCalls() {
+ var group = this.group();
+ var p1 = group(), p2 = group();
+ p1(null, 1);
+ p2(null, 2);
+ },
+ function parallelResults(err, results) {
+ if(err) throw err;
+ fulfill("test5: " + results);
+ return 666;
+ },
+ function terminate1(err, num) {
+ if(err) throw err;
+ fulfill("test5 t1: " + num);
+ var next = this;
+ setTimeout(function() { next(null, 333); }, 50);
+ },
+ function terminate2(err, num) {
+ if(err) throw err;
+ fulfill("test5 t2: " + num);
+ this();
+ }
+);
+}, 1000);
View
60 test/parallelTest.js
@@ -46,4 +46,62 @@ Step(
if(err) throw err;
fulfill("test2: " + num);
}
-)
+)
+
+
+// Test lock functionality with parallel calls with delay
+expect("test3: 1,2");
+expect("test3 t1: 666");
+expect("test3 t2: 333");
+Step(
+ function parallelCalls() {
+ var p1 = this.parallel(), p2 = this.parallel();
+ process.nextTick(function() { p1(null, 1); });
+ process.nextTick(function() { p2(null, 2); });
+ },
+ function parallelResults(err, one, two) {
+ if(err) throw err;
+ fulfill("test3: " + [one, two]);
+ return 666;
+ },
+ function terminate1(err, num) {
+ if(err) throw err;
+ fulfill("test3 t1: " + num);
+ var next = this;
+ setTimeout(function() { next(null, 333); }, 50);
+ },
+ function terminate2(err, num) {
+ if(err) throw err;
+ fulfill("test3 t2: " + num);
+ this();
+ }
+);
+
+
+// Test lock functionality with parallel calls which return immediately
+expect("test4: 1,2");
+expect("test4 t1: 666");
+expect("test4 t2: 333");
+Step(
+ function parallelCalls() {
+ var p1 = this.parallel(), p2 = this.parallel();
+ p1(null, 1);
+ p2(null, 2);
+ },
+ function parallelResults(err, one, two) {
+ if(err) throw err;
+ fulfill("test4: " + [one, two]);
+ return 666;
+ },
+ function terminate1(err, num) {
+ if(err) throw err;
+ fulfill("test4 t1: " + num);
+ var next = this;
+ setTimeout(function() { next(null, 333); }, 50);
+ },
+ function terminate2(err, num) {
+ if(err) throw err;
+ fulfill("test4 t2: " + num);
+ this();
+ }
+);
Please sign in to comment.
Something went wrong with that request. Please try again.