|
| 1 | +var async = require('../async-lite.js'); |
| 2 | +var expect = require('expect.js'); |
| 3 | + |
| 4 | +describe('async.waterfall', function() { |
| 5 | + |
| 6 | + it('should only process task arrays with at least 1 task', function(done) { |
| 7 | + |
| 8 | + async.waterfall([], function(err, results) { |
| 9 | + |
| 10 | + expect(err).to.not.be.ok(); |
| 11 | + expect(results).to.not.be.ok(); |
| 12 | + done(); |
| 13 | + |
| 14 | + }); |
| 15 | + |
| 16 | + }); |
| 17 | + |
| 18 | + it('should only process 1 task in the array at a time', function(done) { |
| 19 | + |
| 20 | + var start_time = Date.now(); |
| 21 | + |
| 22 | + async.series([ |
| 23 | + |
| 24 | + function task1(callback) { |
| 25 | + setTimeout(function() { |
| 26 | + callback(); |
| 27 | + }, 200); |
| 28 | + }, |
| 29 | + |
| 30 | + function task2(callback) { |
| 31 | + setTimeout(function() { |
| 32 | + callback(); |
| 33 | + }, 400); |
| 34 | + }, |
| 35 | + |
| 36 | + function task3(callback) { |
| 37 | + setTimeout(function() { |
| 38 | + callback(); |
| 39 | + }, 600); |
| 40 | + } |
| 41 | + |
| 42 | + ], function(err, results) { |
| 43 | + |
| 44 | + var end_time = Date.now(); |
| 45 | + var time_difference = end_time - start_time; |
| 46 | + |
| 47 | + expect(err).to.not.be.ok(); |
| 48 | + expect(results).to.be.an('array'); |
| 49 | + expect(results[0]).to.not.be.ok(); |
| 50 | + expect(results[1]).to.not.be.ok(); |
| 51 | + expect(results[2]).to.not.be.ok(); |
| 52 | + expect(time_difference).to.be.greaterThan(199); |
| 53 | + done(); |
| 54 | + |
| 55 | + }); |
| 56 | + |
| 57 | + }); |
| 58 | + |
| 59 | + it('should process each task in order', function(done) { |
| 60 | + |
| 61 | + var start_time = Date.now(); |
| 62 | + |
| 63 | + async.waterfall([ |
| 64 | + |
| 65 | + function task1(callback) { |
| 66 | + setTimeout(function() { |
| 67 | + callback(null, ["task1"]); |
| 68 | + }, 600); |
| 69 | + }, |
| 70 | + |
| 71 | + function task2(results, callback) { |
| 72 | + setTimeout(function() { |
| 73 | + callback(null, results.concat("task2")); |
| 74 | + }, 400); |
| 75 | + }, |
| 76 | + |
| 77 | + function task3(results, callback) { |
| 78 | + setTimeout(function() { |
| 79 | + callback(null, results.concat("task3")); |
| 80 | + }, 200); |
| 81 | + } |
| 82 | + |
| 83 | + ], function(err, results) { |
| 84 | + |
| 85 | + var end_time = Date.now(); |
| 86 | + var time_difference = end_time - start_time; |
| 87 | + |
| 88 | + expect(err).to.not.be.ok(); |
| 89 | + expect(results).to.be.an('array'); |
| 90 | + expect(results).to.eql(["task1", "task2", "task3"]); |
| 91 | + expect(time_difference).to.be.greaterThan(1200); |
| 92 | + done(); |
| 93 | + |
| 94 | + }); |
| 95 | + |
| 96 | + }); |
| 97 | + |
| 98 | + it('should stop processing the array of tasks when a task passes a truthy error value to the callback', function(done) { |
| 99 | + |
| 100 | + var start_time = Date.now(); |
| 101 | + |
| 102 | + async.waterfall([ |
| 103 | + |
| 104 | + function task1(callback) { |
| 105 | + setTimeout(function() { |
| 106 | + callback(true, "task1"); |
| 107 | + }, 600); |
| 108 | + }, |
| 109 | + |
| 110 | + function task2(callback) { |
| 111 | + setTimeout(function() { |
| 112 | + callback(null, "task2"); |
| 113 | + }, 400); |
| 114 | + }, |
| 115 | + |
| 116 | + function task3(callback) { |
| 117 | + setTimeout(function() { |
| 118 | + callback(null, "task3"); |
| 119 | + }, 200); |
| 120 | + } |
| 121 | + |
| 122 | + ], function(err, results) { |
| 123 | + |
| 124 | + var end_time = Date.now(); |
| 125 | + var time_difference = end_time - start_time; |
| 126 | + |
| 127 | + expect(err).to.be.ok(); |
| 128 | + expect(results).to.be.ok(); |
| 129 | + expect(time_difference).to.be.lessThan(1000); |
| 130 | + done(); |
| 131 | + |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | +}); |
0 commit comments