Skip to content
This repository
Browse code

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
Seth LaForge authored July 28, 2011
12  lib/step.js
@@ -30,6 +30,7 @@ function Step() {
30 30
 
31 31
   // Define the main callback that's given as `this` to the steps.
32 32
   function next() {
  33
+    counter = pending = 0;
33 34
 
34 35
     // Check if there are no steps left
35 36
     if (steps.length === 0) {
@@ -42,7 +43,6 @@ function Step() {
42 43
 
43 44
     // Get the next step to execute
44 45
     var fn = steps.shift();
45  
-    counter = pending = 0;
46 46
     results = [];
47 47
 
48 48
     // Run the step in a try..catch block so exceptions don't get out of hand.
@@ -54,9 +54,12 @@ function Step() {
54 54
       next(e);
55 55
     }
56 56
 
57  
-
58  
-    // If a syncronous return is used, pass it to the callback
59  
-    if (result !== undefined) {
  57
+    if (counter > 0 && pending == 0) {
  58
+      // If parallel() was called, and all parallel branches executed
  59
+      // syncronously, go on to the next step now.
  60
+      next.apply(null, results);
  61
+    } else if (result !== undefined) {
  62
+      // If a syncronous return is used, pass it to the callback
60 63
       next(undefined, result);
61 64
     }
62 65
     lock = false;
@@ -73,7 +76,6 @@ function Step() {
73 76
         next.apply(null, results);
74 77
       }
75 78
     }
76  
-    process.nextTick(check); // Ensures that check is called at least once
77 79
 
78 80
     return function () {
79 81
       pending--;
34  test/groupTest.js
@@ -99,4 +99,36 @@ Step(
99 99
         if(err) throw err;
100 100
         fulfill("test4: " + num);
101 101
     }
102  
-);
  102
+);
  103
+
  104
+
  105
+// Test lock functionality with groups which return immediately
  106
+expect("test5: 1,2");
  107
+expect("test5 t1: 666");
  108
+expect("test5 t2: 333");
  109
+setTimeout(function() {
  110
+Step(
  111
+  function parallelCalls() {
  112
+    var group = this.group();
  113
+    var p1 = group(), p2 = group();
  114
+    p1(null, 1);
  115
+    p2(null, 2);
  116
+  },
  117
+  function parallelResults(err, results) {
  118
+    if(err) throw err;
  119
+    fulfill("test5: " + results);
  120
+    return 666;
  121
+  },
  122
+  function terminate1(err, num) {
  123
+    if(err) throw err;
  124
+    fulfill("test5 t1: " + num);
  125
+    var next = this;
  126
+    setTimeout(function() { next(null, 333); }, 50);
  127
+  },
  128
+  function terminate2(err, num) {
  129
+    if(err) throw err;
  130
+    fulfill("test5 t2: " + num);
  131
+    this();
  132
+  }
  133
+);
  134
+}, 1000);
60  test/parallelTest.js
@@ -46,4 +46,62 @@ Step(
46 46
         if(err) throw err;
47 47
         fulfill("test2: " + num);
48 48
     }
49  
-)
  49
+)
  50
+
  51
+
  52
+// Test lock functionality with parallel calls with delay
  53
+expect("test3: 1,2");
  54
+expect("test3 t1: 666");
  55
+expect("test3 t2: 333");
  56
+Step(
  57
+  function parallelCalls() {
  58
+    var p1 = this.parallel(), p2 = this.parallel();
  59
+    process.nextTick(function() { p1(null, 1); });
  60
+    process.nextTick(function() { p2(null, 2); });
  61
+  },
  62
+  function parallelResults(err, one, two) {
  63
+    if(err) throw err;
  64
+    fulfill("test3: " + [one, two]);
  65
+    return 666;
  66
+  },
  67
+  function terminate1(err, num) {
  68
+    if(err) throw err;
  69
+    fulfill("test3 t1: " + num);
  70
+    var next = this;
  71
+    setTimeout(function() { next(null, 333); }, 50);
  72
+  },
  73
+  function terminate2(err, num) {
  74
+    if(err) throw err;
  75
+    fulfill("test3 t2: " + num);
  76
+    this();
  77
+  }
  78
+);
  79
+
  80
+
  81
+// Test lock functionality with parallel calls which return immediately
  82
+expect("test4: 1,2");
  83
+expect("test4 t1: 666");
  84
+expect("test4 t2: 333");
  85
+Step(
  86
+  function parallelCalls() {
  87
+    var p1 = this.parallel(), p2 = this.parallel();
  88
+    p1(null, 1);
  89
+    p2(null, 2);
  90
+  },
  91
+  function parallelResults(err, one, two) {
  92
+    if(err) throw err;
  93
+    fulfill("test4: " + [one, two]);
  94
+    return 666;
  95
+  },
  96
+  function terminate1(err, num) {
  97
+    if(err) throw err;
  98
+    fulfill("test4 t1: " + num);
  99
+    var next = this;
  100
+    setTimeout(function() { next(null, 333); }, 50);
  101
+  },
  102
+  function terminate2(err, num) {
  103
+    if(err) throw err;
  104
+    fulfill("test4 t2: " + num);
  105
+    this();
  106
+  }
  107
+);

0 notes on commit 5c56c26

Please sign in to comment.
Something went wrong with that request. Please try again.