1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');
describe("waterfall", function () {
it('basics', function(done){
var call_order = [];
async.waterfall([
function(callback){
call_order.push('fn1');
setTimeout(function(){callback(null, 'one', 'two');}, 0);
},
function(arg1, arg2, callback){
call_order.push('fn2');
expect(arg1).to.equal('one');
expect(arg2).to.equal('two');
setTimeout(function(){callback(null, arg1, arg2, 'three');}, 25);
},
function(arg1, arg2, arg3, callback){
call_order.push('fn3');
expect(arg1).to.equal('one');
expect(arg2).to.equal('two');
expect(arg3).to.equal('three');
callback(null, 'four');
},
function(arg4, callback){
call_order.push('fn4');
expect(call_order).to.eql(['fn1','fn2','fn3','fn4']);
callback(null, 'test');
}
], function(err){
expect(err === null, err + " passed instead of 'null'");
done();
});
});
it('empty array', function(done){
async.waterfall([], function(err){
if (err) throw err;
done();
});
});
it('non-array', function(done){
async.waterfall({}, function(err){
expect(err.message).to.equal('First argument to waterfall must be an array of functions');
done();
});
});
it('no callback', function(done){
async.waterfall([
function(callback){callback();},
function(callback){callback(); done();}
]);
});
it('async', function(done){
var call_order = [];
async.waterfall([
function(callback){
call_order.push(1);
callback();
call_order.push(2);
},
function(callback){
call_order.push(3);
callback();
},
function(){
expect(call_order).to.eql([1,3]);
done();
}
]);
});
it('error', function(done){
async.waterfall([
function(callback){
callback('error');
},
function(callback){
assert(false, 'next function should not be called');
callback();
}
], function(err){
expect(err).to.equal('error');
done();
});
});
it('multiple callback calls', function(){
var arr = [
function(callback){
callback(null, 'one', 'two');
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
callback(null, arg1, arg2, 'three');
}
];
expect(function () {
async.waterfall(arr, function () {});
}).to.throw(/already called/);
});
it('multiple callback calls (trickier) @nodeonly', function(done){
// do a weird dance to catch the async thrown error before mocha
var listeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
process.once('uncaughtException', function onErr(err) {
listeners.forEach(function(listener) {
process.on('uncaughtException', listener);
});
// can't throw errors in a uncaughtException handler, defer
setTimeout(checkErr, 0, err)
})
function checkErr(err) {
expect(err.message).to.match(/already called/);
done();
}
async.waterfall([
function(callback){
setTimeout(callback, 0, null, 'one', 'two');
setTimeout(callback, 2, null, 'one', 'two');
},
function(arg1, arg2, callback){
setTimeout(callback, 15, null, arg1, arg2, 'three');
}
], function () {
throw new Error('should not get here')
});
});
it('call in another context @nycinvalid @nodeonly', function(done) {
var vm = require('vm');
var sandbox = {
async: async,
done: done
};
var fn = "(" + (function () {
async.waterfall([function (callback) {
callback();
}], function (err) {
if (err) {
return done(err);
}
done();
});
}).toString() + "())";
vm.runInNewContext(fn, sandbox);
});
it('should not use unnecessary deferrals', function (done) {
var sameStack = true;
async.waterfall([
function (cb) { cb(null, 1); },
function (arg, cb) { cb(); }
], function() {
expect(sameStack).to.equal(true);
done();
});
sameStack = false;
});
});
|