-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.js
372 lines (307 loc) · 6.79 KB
/
test.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
var tape = require('tape')
var nanoiterator = require('./')
var toStream = require('./to-stream')
tape('basic', function (t) {
var data = ['a', 'b', 'c', null]
var expected = data.slice(0)
var ite = nanoiterator({
next: cb => cb(null, data.shift())
})
ite.next(function loop (err, value) {
t.error(err, 'no error')
t.same(value, expected.shift(), 'expected value')
if (value) ite.next(loop)
else t.end()
})
})
tape('concurrent next', function (t) {
var data = ['a', 'b', 'c', null]
var ite = nanoiterator({
next: function (cb) {
t.ok(data.length > 0, 'has data')
cb(null, data.shift())
}
})
ite.next(function (err, value) {
t.error(err, 'no error')
t.same(value, 'a', 'expected a')
})
ite.next(function (err, value) {
t.error(err, 'no error')
t.same(value, 'b', 'expected b')
})
ite.next(function (err, value) {
t.error(err, 'no error')
t.same(value, 'c', 'expected c')
})
ite.next(function (err, value) {
t.error(err, 'no error')
t.same(value, null, 'expected null')
})
ite.next(function (err, value) {
t.error(err, 'no error')
t.same(value, null, 'expected null')
t.end()
})
})
tape('next inside next cb', function (t) {
var n = 0
var ite = nanoiterator({
next: cb => process.nextTick(cb, null, n++)
})
ite.next(function (err, n) {
t.error(err, 'no error')
t.same(n, 0)
ite.next(function (err, n) {
t.error(err, 'no error')
t.same(n, 1)
})
ite.next(function (err, n) {
t.error(err, 'no error')
t.same(n, 2)
t.end()
})
})
})
tape('open', function (t) {
t.plan(4 + 2 + 2)
var cnt = 0
var ite = nanoiterator({
open: function (cb) {
t.notOk(ite.opened, '.opened set after open')
t.pass('was opened')
cb()
},
next: function (cb) {
t.ok(ite.opened, 'is opened')
cb(null, cnt++)
}
})
ite.next(function (err, val) {
t.error(err, 'no error')
t.same(val, 0)
ite.next(function (err, val) {
t.error(err, 'no error')
t.same(val, 1)
})
})
})
tape('no next', function (t) {
t.plan(2)
var ite = nanoiterator()
ite.next(function (err) {
t.same(err, new Error('_next was not implemented'))
})
ite = nanoiterator({})
ite.next(function (err) {
t.same(err, new Error('_next was not implemented'))
})
})
tape('destroy', function (t) {
t.plan(3)
var ite = nanoiterator({
destroy: function (cb) {
t.pass('_destroy is called')
cb()
}
})
ite.destroy(function () {
t.ok(ite.closed, 'destroyed')
})
ite.next(function (err) {
t.same(err, new Error('Iterator is destroyed'))
})
})
tape('destroy while next', function (t) {
t.plan(3)
var ite = nanoiterator({
next: function (cb) {
t.fail('_next is never called')
},
destroy: function (cb) {
t.pass('_destroy is called')
cb()
}
})
ite.destroy(function () {
t.ok(ite.closed, 'destroyed')
})
ite.next(function (err) {
t.same(err, new Error('Iterator is destroyed'))
})
})
tape('open fails', function (t) {
var ite = nanoiterator({
open: function (cb) {
cb(new Error('open fails'))
},
next: function () {
t.fail('next should not be called')
}
})
ite.next(function (err) {
t.same(err, new Error('open fails'))
t.end()
})
})
tape('destroy optional callback', function (t) {
t.plan(3)
var ite = nanoiterator({
next: function (cb) {
t.pass('_next is called')
process.nextTick(cb)
},
destroy: function (cb) {
t.pass('_destroy is called')
cb()
}
})
ite.next(function (err) {
t.same(err, new Error('Iterator is destroyed'))
})
ite.destroy()
})
tape('destroy with default _destroy', function (t) {
t.plan(2)
var ite = nanoiterator()
ite.destroy(function () {
t.ok(ite.closed, 'should be closed')
})
ite.next(function (err) {
t.same(err, new Error('Iterator is destroyed'))
})
})
tape('destroy twice', function (t) {
t.plan(5)
var ite = nanoiterator()
var first = true
ite.destroy(function () {
t.ok(first, 'is first')
t.ok(ite.closed, 'should be closed')
first = false
})
ite.destroy(function () {
t.notOk(first, 'is not first')
t.ok(ite.closed, 'should be closed')
})
ite.next(function (err) {
t.same(err, new Error('Iterator is destroyed'))
})
})
tape('to-stream', function (t) {
var data = ['a', 'b', 'c', null]
var expected = data.slice(0, -1)
var ite = nanoiterator({
next: cb => cb(null, data.shift())
})
var s = toStream(ite)
s.on('data', function (data) {
t.same(data, expected.shift())
})
s.on('end', function () {
t.same(expected.length, 0)
t.end()
})
})
tape('to-stream error', function (t) {
t.plan(3)
var ite = nanoiterator({
next: cb => cb(new Error('stop')),
destroy: function (cb) {
t.pass('destroy called')
cb()
}
})
var s = toStream(ite)
s.on('error', function (err) {
t.same(err, new Error('stop'))
})
s.on('close', function () {
t.pass('closed')
})
s.resume()
})
tape('to-stream destroy', function (t) {
t.plan(3)
var ite = nanoiterator({
next: cb => cb(null, null),
destroy: function (cb) {
t.pass('destroy called')
cb()
}
})
var s = toStream(ite)
s.on('error', function (err) {
t.same(err, new Error('stop'))
})
s.on('close', function () {
t.pass('closed')
})
s.destroy(new Error('stop'))
})
tape('to-stream destroy errors', function (t) {
t.plan(3)
var ite = nanoiterator({
next: cb => cb(null, 'hi'),
destroy: function (cb) {
t.pass('destroy called')
cb(new Error('stop'))
}
})
var s = toStream(ite)
s.on('error', function (err) {
t.same(err, new Error('stop'))
})
s.on('close', function () {
t.pass('closed')
})
s.destroy()
})
tape('to-stream destroy twice', function (t) {
t.plan(3)
var ite = nanoiterator({
next: cb => cb(null, 'hi'),
destroy: function (cb) {
t.pass('destroy called')
cb(new Error('stop'))
}
})
var s = toStream(ite)
s.on('error', function (err) {
t.same(err, new Error('stop'))
})
s.on('close', function () {
t.pass('closed')
})
s.destroy()
s.destroy()
})
tape('always async', function (t) {
var ite = nanoiterator({
next: cb => cb(null, 'hi')
})
var sync = true
ite.next(function () {
t.notOk(sync, 'not sync')
sync = true
ite.destroy(function () {
t.notOk(sync, 'not sync')
t.end()
})
sync = false
})
sync = false
})
tape('not double async', function (t) {
var ite = nanoiterator({
next: cb => process.nextTick(cb, null, 'hi')
})
var flag = false
ite.next(function () {
t.notOk(flag, 'not set')
t.end()
})
process.nextTick(function () {
flag = true
})
})