summaryrefslogtreecommitdiff
path: root/test/times.js
blob: ad1dfcdf4c50524298b617758105629f6af18018 (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
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');

describe('times', function() {

    it('times', function(done) {
        async.times(5, function(n, next) {
            next(null, n);
        }, function(err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(results).to.eql([0,1,2,3,4]);
            done();
        });
    });

    it('times 3', function(done) {
        var args = [];
        async.times(3, function(n, callback){
            setTimeout(function(){
                args.push(n);
                callback();
            }, n * 25);
        }, function(err){
            if (err) throw err;
            expect(args).to.eql([0,1,2]);
            done();
        });
    });

    it('times 0', function(done) {
        async.times(0, function(n, callback){
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err){
            if (err) throw err;
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('times error', function(done) {
        async.times(3, function(n, callback){
            callback('error');
        }, function(err){
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('timesSeries', function(done) {
        var call_order = [];
        async.timesSeries(5, function(n, callback){
            setTimeout(function(){
                call_order.push(n);
                callback(null, n);
            }, 100 - n * 10);
        }, function(err, results){
            expect(call_order).to.eql([0,1,2,3,4]);
            expect(results).to.eql([0,1,2,3,4]);
            done();
        });
    });

    it('timesSeries error', function(done) {
        async.timesSeries(5, function(n, callback){
            callback('error');
        }, function(err){
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('timesLimit', function(done) {
        var limit = 2;
        var running = 0;
        async.timesLimit(5, limit, function (i, next) {
            running++;
            assert(running <= limit && running > 0, running);
            setTimeout(function () {
                running--;
                next(null, i * 2);
            }, (3 - i) * 10);
        }, function(err, results){
            assert(err === null, err + " passed instead of 'null'");
            expect(results).to.eql([0, 2, 4, 6, 8]);
            done();
        });
    });
});