Example #1
0
    it('should render a series of sync and async calls correctly', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut1 = out.beginAsync();
        setTimeout(function () {
            asyncOut1.write('2');
            asyncOut1.end();
        }, 100);

        out.write('3');

        var asyncOut2 = out.beginAsync();
        setTimeout(function () {
            asyncOut2.write('4');
            asyncOut2.end();
        }, 10);

        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('1234');
            done();
        });
    });
Example #2
0
    it('should allow an async fragment to flush last asynchronously', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut = out.beginAsync({ last: true });
        var lastFiredCount = 0;

        out.on('last', function () {
            lastFiredCount++;
        });

        out.once('last', function () {
            setTimeout(function () {
                asyncOut.write('2');
                asyncOut.end();
            }, 10);
        });

        out.write('3');
        out.end();
        out.on('finish', function (result) {
            expect(lastFiredCount).to.equal(1);
            var output = result.getOutput();
            expect(output).to.equal('123');
            done();
        });
    });
Example #3
0
    it('should render nested async calls correctly', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut = out.beginAsync();
        setTimeout(function () {
            asyncOut.write('2a');

            var nestedAsyncContext = asyncOut.beginAsync();
            setTimeout(function () {
                nestedAsyncContext.write('2b');
                nestedAsyncContext.end();
            }, 10);

            asyncOut.write('2c');
            asyncOut.end();
        }, 10);

        out.write('3');

        var asyncOut2 = out.beginAsync();
        setTimeout(function () {
            var nestedAsyncContext = asyncOut2.beginAsync();
            setTimeout(function () {
                nestedAsyncContext.write('4a');
                nestedAsyncContext.end();
            }, 10);

            var nestedAsyncContext2 = asyncOut2.beginAsync();
            setTimeout(function () {
                nestedAsyncContext2.write('4b');
                nestedAsyncContext2.end();
            }, 10);

            asyncOut2.write('4c');
            asyncOut2.end();
        }, 10);

        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('12a2b2c34a4b4c');
            done();
        });
    });
Example #4
0
        setTimeout(function () {
            var asyncOut = out.beginAsync({ name: 'inner' });
            setTimeout(function () {
                asyncOut.write('2');
                asyncOut.end();
            }, 50);

            out.write('3');

            out.end();
        }, 10);
Example #5
0
    it('should allow multiple onLast calls', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut1 = out.beginAsync();
        setTimeout(function () {
            asyncOut1.write('2');
            asyncOut1.end();
        }, 20);

        var asyncOut2 = out.beginAsync({ last: true });
        var onLastCount = 0;
        var lastOutput = [];

        out.onLast(function (next) {
            onLastCount++;
            lastOutput.push('a');
            asyncOut2.write('3');
            asyncOut2.end();
            setTimeout(next, 10);
        });

        var asyncOut3 = out.beginAsync({ last: true });
        out.onLast(function (next) {
            onLastCount++;
            lastOutput.push('b');
            asyncOut3.write('4');
            asyncOut3.end();
            next();
        });

        out.write('5');
        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('12345');
            expect(onLastCount).to.equal(2);
            expect(lastOutput).to.deep.equal(['a', 'b']);
            done();
        });
    });
Example #6
0
    it('should allow the async callback to provide data', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut = out.beginAsync();
        setTimeout(function () {
            asyncOut.end('2');
        }, 10);

        out.write('3');
        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('123');
            done();
        });
    });
Example #7
0
    it('should allow an async fragment to flush last', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut = out.beginAsync({ last: true });
        out.once('last', function () {
            asyncOut.write('2');
            asyncOut.end();
        });

        out.write('3');
        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('123');
            done();
        });
    });
Example #8
0
    it('should support piping to an async writer', function (done) {

        var out = new AsyncStream();
        out.write('1');

        var asyncOut = out.beginAsync();

        var helloReadStream = fs.createReadStream(nodePath.join(__dirname, 'fixtures/AsyncStream/hello.txt'), fsReadOptions);
        helloReadStream.pipe(asyncOut);

        out.write('2');
        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('1Hello World2');
            done();
        });
    });
Example #9
0
    it('should allow an async fragment to complete synchronously', function (done) {
        var out = new AsyncStream();
        out.write('1');

        var asyncOut = out.beginAsync();
        setTimeout(function () {
            asyncOut.write('2');
            asyncOut.end();
        }, 10);

        out.write('3');
        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(output).to.equal('123');
            done();
        });
    });
Example #10
0
    it('should handle sync errors correctly', function (done) {
        var out = new AsyncStream();
        var errors = [];
        out.on('error', function (e) {
            errors.push(e);
        });

        out.write('1');

        var asyncOut = out.beginAsync();
        setTimeout(function () {
            asyncOut.error(new Error('test'));
        }, 10);
        out.write('3');
        out.end();
        out.on('finish', function (result) {
            var output = result.getOutput();
            expect(errors.length).to.equal(1);
            expect(output).to.equal('13');
            done();
        });
    });
Example #11
0
    it('should catch error in promise catch', function (done) {
        const out = new AsyncStream();

        let errors = [];

        out.on('error', function (e) {
            errors.push(e);
        });

        out.write('1');

        let asyncOut = out.beginAsync();
        setTimeout(function () {
            asyncOut.error(new Error('test'));
        }, 10);

        out.write('3');
        out.end().catch(err => {
            expect(errors.length).to.equal(1);
            expect(err).to.be.an('error');
            done();
        });
    });
Example #12
0
    it('should handle timeouts correctly', function (done) {
        var out = new AsyncStream();
        var errors = [];
        out.on('error', function (e) {
            errors.push(e);
        });

        out.write('1');

        var asyncOut = out.beginAsync({ timeout: 100, name: 'test' });
        setTimeout(function () {
            asyncOut.write('2');
            asyncOut.end();
        }, 200);

        out.write('3');
        out.end();

        out.on('finish', function (result) {
            expect(errors.length).to.equal(1);
            expect(result.getOutput()).to.equal('13');
            done();
        });
    });