File: settleParallel.js

package info (click to toggle)
node-gulp 4.0.2%2B~cs54.26.36-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,596 kB
  • sloc: javascript: 1,133,545; sh: 12; makefile: 6
file content (70 lines) | stat: -rw-r--r-- 1,621 bytes parent folder | download | duplicates (2)
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
'use strict';

var expect = require('expect');

var bach = require('../');

function fn1(done) {
  done(null, 1);
}

function fn2(done) {
  setTimeout(function() {
    done(null, 2);
  }, 500);
}

function fn3(done) {
  done(null, 3);
}

function fnError(done) {
  done(new Error('An Error Occurred'));
}

describe('settleParallel', function() {

  it('should execute functions in parallel, passing settled results', function(done) {
    bach.settleParallel(fn1, fn2, fn3)(function(errors, results) {
      expect(errors).toEqual(null);
      expect(results).toEqual([1, 2, 3]);
      done();
    });
  });

  it('should execute functions in parallel, passing settled errors and results', function(done) {
    function slowFn(done) {
      setTimeout(function() {
        done(null, 2);
      }, 500);
    }
    bach.settleParallel(fn1, slowFn, fn3, fnError)(function(errors, results) {
      expect(errors).toBeAn(Array);
      expect(errors[0]).toBeAn(Error);
      expect(results).toEqual([1, 2, 3]);
      done();
    });
  });

  it('should take extension points and call them for each function', function(done) {
    var arr = [];
    var fns = [fn1, fn2, fn3];
    bach.settleParallel(fn1, fn2, fn3, {
      create: function(fn, idx) {
        expect(fns).toInclude(fn);
        arr[idx] = fn;
        return arr;
      },
      before: function(storage) {
        expect(storage).toEqual(arr);
      },
      after: function(result, storage) {
        expect(storage).toEqual(arr);
      },
    })(function(error) {
      expect(error).toEqual(null);
      expect(arr).toEqual(fns);
    });
    done();
  });
});