git ssb

3+

cel / ssb-npm-registry



Tree: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3

Files: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3 / node_modules / pull-stream / test / async-map.js

3114 bytesRaw
1var pull = require('../')
2var tape = require('tape')
3tape('async-map', function (t) {
4
5 pull(
6 pull.count(),
7 pull.take(21),
8 pull.asyncMap(function (data, cb) {
9 return cb(null, data + 1)
10 }),
11 pull.collect(function (err, ary) {
12 console.log(ary)
13 t.equal(ary.length, 21)
14 t.end()
15 })
16 )
17})
18
19tape('abort async map', function (t) {
20 var err = new Error('abort')
21 t.plan(2)
22
23 var read = pull(
24 pull.infinite(),
25 pull.asyncMap(function (data, cb) {
26 setImmediate(function () {
27 cb(null, data)
28 })
29 })
30 )
31
32 read(null, function (end) {
33 if(!end) throw new Error('expected read to end')
34 t.ok(end, "read's callback")
35 })
36
37 read(err, function (end) {
38 if(!end) throw new Error('expected abort to end')
39 t.ok(end, "Abort's callback")
40 t.end()
41 })
42
43})
44
45tape('abort async map (source is slow to ack abort)', function (t) {
46 var err = new Error('abort')
47 t.plan(3)
48
49 function source(end, cb) {
50 if (end) setTimeout(function () { cb(end) }, 20)
51 else cb(null, 10)
52 }
53
54 var read = pull(
55 source,
56 pull.asyncMap(function (data, cb) {
57 setImmediate(function () {
58 cb(null, data)
59 })
60 })
61 )
62
63 var ended = false
64
65 read(null, function (end) {
66 if(!end) throw new Error('expected read to end')
67 ended = true
68 t.ok(end, "read's callback")
69 })
70
71 read(err, function (end) {
72 if(!end) throw new Error('expected abort to end')
73 t.ok(end, "Abort's callback")
74 t.ok(ended, 'read called back first')
75 t.end()
76 })
77
78})
79
80tape('abort async map (async source)', function (t) {
81 var err = new Error('abort')
82 t.plan(2)
83
84 var read = pull(
85 function(err, cb) {
86 setImmediate(function() {
87 if (err) return cb(err)
88 cb(null, 'x')
89 })
90 },
91 pull.asyncMap(function (data, cb) {
92 setImmediate(function () {
93 cb(null, data)
94 })
95 })
96 )
97
98 read(null, function (end) {
99 if(!end) throw new Error('expected read to end')
100 t.ok(end, "read's callback")
101 })
102
103 read(err, function (end) {
104 if(!end) throw new Error('expected abort to end')
105 t.ok(end, "Abort's callback")
106 t.end()
107 })
108
109})
110tape('asyncMap aborts when map errors', function (t) {
111 t.plan(2)
112 var ERR = new Error('abort')
113 pull(
114 pull.values([1,2,3], function (err) {
115 console.log('on abort')
116 t.equal(err, ERR, 'abort gets error')
117 t.end()
118 }),
119 pull.asyncMap(function (data, cb) {
120 cb(ERR)
121 }),
122 pull.collect(function (err) {
123 t.equal(err, ERR, 'collect gets error')
124 })
125 )
126})
127
128tape("async map should pass its own error", function (t) {
129 var i = 0
130 var error = new Error('error on last call')
131
132 pull(
133 function (end, cb) {
134 end ? cb(true) : cb(null, i+1)
135 },
136 pull.asyncMap(function (data, cb) {
137 setTimeout(function () {
138 if(++i < 5) cb(null, data)
139 else {
140 cb(error)
141 }
142 }, 100)
143 }),
144 pull.collect(function (err, five) {
145 t.equal(err, error, 'should return err')
146 t.deepEqual(five, [1,2,3,4], 'should skip failed item')
147 t.end()
148 })
149 )
150})
151
152
153

Built with git-ssb-web