This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
85 lines (70 loc) · 2.04 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
const test = require('ava')
const AWSApiReadStream = require('./index')
const tokens = 'abcdefghij'.split('')
test('AWSApiReadStream', async t => {
const testapi = new TestAPI()
const stream = AWSApiReadStream.from(nextToken => testapi.anAPICall(nextToken))
let count = 0
for await (const { data, NextToken } of stream) {
t.is(data, count)
count++
}
t.deepEqual(testapi.spy, [
{ nextToken: undefined, data: 0 },
{ nextToken: 'a', data: 1 },
{ nextToken: 'b', data: 2 },
{ nextToken: 'c', data: 3 },
{ nextToken: 'd', data: 4 },
{ nextToken: 'e', data: 5 },
{ nextToken: 'f', data: 6 },
{ nextToken: 'g', data: 7 },
{ nextToken: 'h', data: 8 },
{ nextToken: 'i', data: 9 },
{ nextToken: 'j', data: 10 }
])
})
test('AWSApiReadStream - initialize with existing token', async t => {
const testapi = new TestAPI()
const stream = AWSApiReadStream.from(nextToken => testapi.anAPICall(nextToken), { nextToken: 'f' })
// can't think of a better way to test this other than check internals...
t.is(stream._nextToken, 'f')
})
test('AWSApiReadStream - returning null or undefined will stop execution', async t => {
const testapi = new TestAPI()
const stream = AWSApiReadStream.from(nextToken => nextToken === 'b' ? null : testapi.anAPICall(nextToken))
const results = []
for await (const { data, NextToken } of stream) {
results.push(data)
}
t.deepEqual(results, [0, 1])
})
test.skip('AWSApiReadStream - backpressure', async t => {
const testapi = new TestAPI()
testapi.push = chunk => {
testapi.super.push(chunk)
return count++ !== 2
}
const stream = AWSApiReadStream.from(nextToken => testapi.anAPICall(nextToken))
const results = []
for await (const { data, NextToken } of stream) {
results.push(data)
}
console.log(results)
})
class TestAPI {
constructor() {
this._counter = 0
this.spy = []
}
anAPICall(nextToken) {
this.spy.push({ nextToken, data: this._counter })
return new Promise(res => {
setImmediate(() => {
res({
NextToken: tokens[this._counter],
data: this._counter++
})
})
})
}
}