summaryrefslogtreecommitdiff
path: root/mocha_test/auto.js
blob: d0141bb2c58e0409db0106ff4a2c9a86b4fe7764 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
var async = require('../lib');
var expect = require('chai').expect;
var _ = require("lodash");

describe("auto", function () {

    it('auto', function(done){
        var callOrder = [];
        async.auto({
            task1: ['task2', function(results, callback){
                setTimeout(function(){
                    callOrder.push('task1');
                    callback();
                }, 25);
            }],
            task2: function(callback){
                setTimeout(function(){
                    callOrder.push('task2');
                    callback();
                }, 50);
            },
            task3: ['task2', function(results, callback){
                callOrder.push('task3');
                callback();
            }],
            task4: ['task1', 'task2', function(results, callback){
                callOrder.push('task4');
                callback();
            }],
            task5: ['task2', function(results, callback){
                setTimeout(function(){
                    callOrder.push('task5');
                    callback();
                }, 0);
            }],
            task6: ['task2', function(results, callback){
                callOrder.push('task6');
                callback();
            }]
        },
        function(err){
            expect(err).to.equal(null);
            expect(callOrder).to.eql(['task2','task6','task3','task5','task1','task4']);
            done();
        });
    });

    it('auto concurrency', function (done) {
        var concurrency = 2;
        var runningTasks = [];

        function makeCallback(taskName) {
            return function(/*..., callback*/) {
                var callback = _.last(arguments);
                runningTasks.push(taskName);
                setTimeout(function(){
                    // Each task returns the array of running tasks as results.
                    var result = runningTasks.slice(0);
                    runningTasks.splice(runningTasks.indexOf(taskName), 1);
                    callback(null, result);
                });
            };
        }

        async.auto({
            task1: ['task2', makeCallback('task1')],
            task2: makeCallback('task2'),
            task3: ['task2', makeCallback('task3')],
            task4: ['task1', 'task2', makeCallback('task4')],
            task5: ['task2', makeCallback('task5')],
            task6: ['task2', makeCallback('task6')]
        }, concurrency, function(err, results){
            _.each(results, function(result) {
                expect(result.length).to.be.below(concurrency + 1);
            });
            done();
        });
    });

    it('auto petrify', function (done) {
        var callOrder = [];
        async.auto({
            task1: ['task2', function (results, callback) {
                setTimeout(function () {
                    callOrder.push('task1');
                    callback();
                }, 100);
            }],
            task2: function (callback) {
                setTimeout(function () {
                    callOrder.push('task2');
                    callback();
                }, 200);
            },
            task3: ['task2', function (results, callback) {
                callOrder.push('task3');
                callback();
            }],
            task4: ['task1', 'task2', function (results, callback) {
                callOrder.push('task4');
                callback();
            }]
        },
        function (err) {
            if (err) throw err;
            expect(callOrder).to.eql(['task2', 'task3', 'task1', 'task4']);
            done();
        });
    });

    it('auto results', function(done){
        var callOrder = [];
        async.auto({
            task1: ['task2', function(results, callback){
              expect(results.task2).to.eql('task2');
              setTimeout(function(){
                  callOrder.push('task1');
                  callback(null, 'task1a', 'task1b');
              }, 25);
          }],
            task2: function(callback){
              setTimeout(function(){
                  callOrder.push('task2');
                  callback(null, 'task2');
              }, 50);
          },
            task3: ['task2', function(results, callback){
              expect(results.task2).to.eql('task2');
              callOrder.push('task3');
              callback(null);
          }],
            task4: ['task1', 'task2', function(results, callback){
              expect(results.task1).to.eql(['task1a','task1b']);
              expect(results.task2).to.eql('task2');
              callOrder.push('task4');
              callback(null, 'task4');
          }]
        },
        function(err, results){
            expect(callOrder).to.eql(['task2','task3','task1','task4']);
            expect(results).to.eql({task1: ['task1a','task1b'], task2: 'task2', task3: undefined, task4: 'task4'});
            done();
        });
    });

    it('auto empty object', function(done){
        async.auto({}, function(err){
            expect(err).to.equal(null);
            done();
        });
    });

    it('auto error', function(done){
        async.auto({
            task1: function(callback){
                callback('testerror');
            },
            task2: ['task1', function(results, callback){
                throw new Error('task2 should not be called');
            }],
            task3: function(callback){
                callback('testerror2');
            }
        },
        function(err){
            expect(err).to.equal('testerror');
        });
        setTimeout(done, 100);
    });

    it('auto no callback', function(done){
        async.auto({
            task1: function(callback){callback();},
            task2: ['task1', function(results, callback){callback(); done();}]
        });
    });

    it('auto concurrency no callback', function(done){
        async.auto({
            task1: function(callback){callback();},
            task2: ['task1', function(results, callback){callback(); done();}]
        }, 1);
    });

    it('auto error should pass partial results', function(done) {
        async.auto({
            task1: function(callback){
                callback(false, 'result1');
            },
            task2: ['task1', function(results, callback){
                callback('testerror', 'result2');
            }],
            task3: ['task2', function(){
                throw new Error('task3 should not be called');
            }]
        },
        function(err, results){
            expect(err).to.equal('testerror');
            expect(results.task1).to.equal('result1');
            expect(results.task2).to.equal('result2');
            done();
        });
    });

    // Issue 24 on github: https://github.com/caolan/async/issues#issue/24
    // Issue 76 on github: https://github.com/caolan/async/issues#issue/76
    it('auto removeListener has side effect on loop iterator', function(done) {
        async.auto({
            task1: ['task3', function(/*callback*/) { done(); }],
            task2: ['task3', function(/*callback*/) { /* by design: DON'T call callback */ }],
            task3: function(callback) { callback(); }
        });
    });

    // Issue 410 on github: https://github.com/caolan/async/issues/410
    it('auto calls callback multiple times', function(done) {
        if (process.browser) {
            // node only test
            return done();
        }
        var finalCallCount = 0;
        var domain = require('domain').create();
        domain.on('error', function (e) {
            // ignore test error
            if (!e._test_error) {
                return done(e);
            }
        });
        domain.run(function () {
            async.auto({
                task1: function(callback) { callback(null); },
                task2: ['task1', function(results, callback) { callback(null); }]
            },

            // Error throwing final callback. This should only run once
            function() {
                finalCallCount++;
                var e = new Error("An error");
                e._test_error = true;
                throw e;
            });
        });
        setTimeout(function () {
            expect(finalCallCount).to.equal(1);
            done();
        }, 10);
    });


    it('auto calls callback multiple times with parallel functions', function(done) {
        async.auto({
            task1: function(callback) { setTimeout(callback,0,"err"); },
            task2: function(callback) { setTimeout(callback,0,"err"); }
        },
        // Error throwing final callback. This should only run once
        function(err) {
            expect(err).to.equal("err");
            done();
        });
    });


    // Issue 462 on github: https://github.com/caolan/async/issues/462
    it('auto modifying results causes final callback to run early', function(done) {
        async.auto({
            task1: function(callback, results){
                results.inserted = true;
                callback(null, 'task1');
            },
            task2: function(callback){
                setTimeout(function(){
                    callback(null, 'task2');
                }, 50);
            },
            task3: function(callback){
                setTimeout(function(){
                    callback(null, 'task3');
                }, 100);
            }
        },
        function(err, results){
            expect(results.inserted).to.equal(true);
            expect(results.task3).to.equal('task3');
            done();
        });
    });

    // Issue 263 on github: https://github.com/caolan/async/issues/263
    it('auto prevent dead-locks due to inexistant dependencies', function(done) {
        expect(function () {
            async.auto({
                task1: ['noexist', function(results, callback){
                    callback(null, 'task1');
                }]
            });
        }).to.throw;
        done();
    });

    // Issue 263 on github: https://github.com/caolan/async/issues/263
    it('auto prevent dead-locks due to cyclic dependencies', function(done) {
        expect(function () {
            async.auto({
                task1: ['task2', function(results, callback){
                    callback(null, 'task1');
                }],
                task2: ['task1', function(results, callback){
                    callback(null, 'task2');
                }]
            });
        }).to.throw;
        done();
    });

    // Issue 988 on github: https://github.com/caolan/async/issues/988
    it('auto stops running tasks on error', function(done) {
        async.auto({
            task1: function (callback) {
                callback('error');
            },
            task2: function (callback) {
                throw new Error('test2 should not be called');
            }
        }, 1, function (error) {
            expect(error).to.equal('error');
            done();
        });
    });

});