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

describe('forever', () => {
    context('function is asynchronous', () => {
        it('executes the function over and over until it yields an error', (done) => {
            var counter = 0;
            function addOne(callback) {
                counter++;
                if (counter === 50) {
                    return callback('too big!');
                }
                async.setImmediate(() => {
                    callback();
                });
            }
            async.forever(addOne, (err) => {
                expect(err).to.eql('too big!');
                expect(counter).to.eql(50);
                done();
            });
        });
    });

    context('function is synchronous', () => {
        it('does not cause a stack overflow @nodeonly', (done) => { // this will take forever in a browser
            var counter = 0;
            function addOne(callback) {
                counter++;
                if (counter === 50000) { // needs to be huge to potentially overflow stack in node
                    return callback('too big!');
                }
                callback();
            }
            async.forever(addOne, (err) => {
                expect(err).to.eql('too big!');
                expect(counter).to.eql(50000);
                done();
            });
        });

        it('should cancel', (done) => {
            var counter = 0;
            async.forever(cb => {
                counter++
                cb(counter === 2 ? false : null)
            }, () => { throw new Error('should not get here') })

            setTimeout(() => {
                expect(counter).to.eql(2)
                done()
            }, 10)
        })
    });
});