|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var lab = exports.lab = require('lab').script(); |
| 4 | +var describe = lab.describe; |
| 5 | +var it = lab.it; |
| 6 | +var before = lab.before; |
| 7 | +var beforeEach = lab.beforeEach; |
| 8 | +var after = lab.after; |
| 9 | +var afterEach = lab.afterEach; |
| 10 | +var expect = require('lab').expect; |
| 11 | + |
| 12 | +var Undertaker = require('../'); |
| 13 | + |
| 14 | +function fn1(done){ |
| 15 | + done(null, 1); |
| 16 | +} |
| 17 | + |
| 18 | +function fn2(done){ |
| 19 | + setTimeout(function(){ |
| 20 | + done(null, 2); |
| 21 | + }, 500); |
| 22 | +} |
| 23 | + |
| 24 | +function fn3(done){ |
| 25 | + done(null, 3); |
| 26 | +} |
| 27 | + |
| 28 | +function fnError(done){ |
| 29 | + done(new Error('An Error Occurred')); |
| 30 | +} |
| 31 | + |
| 32 | +describe('parallel', function(){ |
| 33 | + |
| 34 | + var taker; |
| 35 | + |
| 36 | + beforeEach(function(done){ |
| 37 | + taker = new Undertaker(); |
| 38 | + taker.task('test1', fn1); |
| 39 | + taker.task('test2', fn2); |
| 40 | + taker.task('test3', fn3); |
| 41 | + taker.task('error', fnError); |
| 42 | + done(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should take all string names', function(done){ |
| 46 | + taker.parallel('test1', 'test2', 'test3')(function(err, results){ |
| 47 | + expect(results).to.deep.equal([1, 2, 3]); |
| 48 | + done(err); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should take all functions', function(done){ |
| 53 | + taker.parallel(fn1, fn2, fn3)(function(err, results){ |
| 54 | + expect(results).to.deep.equal([1, 2, 3]); |
| 55 | + done(err); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should take string names and functions', function(done){ |
| 60 | + taker.parallel('test1', fn2, 'test3')(function(err, results){ |
| 61 | + expect(results).to.deep.equal([1, 2, 3]); |
| 62 | + done(err); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should take nested parallel', function(done){ |
| 67 | + var parallel1 = taker.parallel('test1', 'test2', 'test3'); |
| 68 | + taker.parallel('test1', parallel1, 'test3')(function(err, results){ |
| 69 | + expect(results).to.deep.equal([1, [1, 2, 3], 3]); |
| 70 | + done(err); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should stop processing on error', function(done){ |
| 75 | + taker.on('error', function(err){ |
| 76 | + // to keep the process from crashing |
| 77 | + }); |
| 78 | + taker.parallel('test1', 'error', 'test3')(function(err, results){ |
| 79 | + expect(err).to.be.an.instanceof(Error); |
| 80 | + expect(results).to.deep.equal([1, undefined, undefined]); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw on unregistered task', function(done){ |
| 86 | + function unregistered(){ |
| 87 | + taker.parallel('unregistered'); |
| 88 | + } |
| 89 | + |
| 90 | + expect(unregistered).to.throw('Task never defined: unregistered'); |
| 91 | + done(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments