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

describe('seq', () => {

    it('seq', (done) => {
        var add2 = function (n, cb) {
            expect(n).to.equal(3);
            setTimeout(() => {
                cb(null, n + 2);
            }, 50);
        };
        var mul3 = function (n, cb) {
            expect(n).to.equal(5);
            setTimeout(() => {
                cb(null, n * 3);
            }, 15);
        };
        var add1 = function (n, cb) {
            expect(n).to.equal(15);
            setTimeout(() => {
                cb(null, n + 1);
            }, 100);
        };
        var add2mul3add1 = async.seq(add2, mul3, add1);
        add2mul3add1(3, (err, result) => {
            if (err) {
                return done(err);
            }
            assert(err === null, err + " passed instead of 'null'");
            expect(result).to.equal(16);
            done();
        });
    });

    it('seq error', (done) => {
        var testerr = new Error('test');

        var add2 = function (n, cb) {
            expect(n).to.equal(3);
            setTimeout(() => {
                cb(null, n + 2);
            }, 50);
        };
        var mul3 = function (n, cb) {
            expect(n).to.equal(5);
            setTimeout(() => {
                cb(testerr);
            }, 15);
        };
        var add1 = function (n, cb) {
            assert(false, 'add1 should not get called');
            setTimeout(() => {
                cb(null, n + 1);
            }, 100);
        };
        var add2mul3add1 = async.seq(add2, mul3, add1);
        add2mul3add1(3, (err) => {
            expect(err).to.equal(testerr);
            done();
        });
    });

    it('seq canceled', (done) => {
        var call_order = [];

        var add2 = function (n, cb) {
            call_order.push('add2');
            cb(null, n + 2);
        };
        var mul3 = function (n, cb) {
            call_order.push('mul3');
            cb(false, n * 3);
        };
        var add1 = function () {
            throw new Error('add1 - should not get here');
        };
        var add2mul3add1 = async.seq(add2, mul3, add1);
        add2mul3add1(3, () => {
            throw new Error('final callback - should not get here');
        });

        setTimeout(() => {
            expect(call_order).to.eql(['add2', 'mul3']);
            done();
        }, 25);
    });

    it('seq binding', (done) => {
        var testcontext = {name: 'foo'};

        var add2 = function (n, cb) {
            expect(this).to.equal(testcontext);
            setTimeout(() => {
                cb(null, n + 2);
            }, 50);
        };
        var mul3 = function (n, cb) {
            expect(this).to.equal(testcontext);
            setTimeout(() => {
                cb(null, n * 3);
            }, 15);
        };
        var add2mul3 = async.seq(add2, mul3);
        add2mul3.call(testcontext, 3, (err, result) => {
            if (err) {
                return done(err);
            }
            expect(result).to.equal(15);
            done();
        });
    });

    it('seq without callback', (done) => {
        var testcontext = {name: 'foo'};

        var add2 = function (n, cb) {
            expect(this).to.equal(testcontext);
            setTimeout(() => {
                cb(null, n + 2);
            }, 50);
        };
        var mul3 = function () {
            expect(this).to.equal(testcontext);
            setTimeout(() => {
                done();
            }, 15);
        };
        var add2mul3 = async.seq(add2, mul3);
        add2mul3.call(testcontext, 3);
    });
});