-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKinesisStream.test.js
165 lines (151 loc) · 4.88 KB
/
KinesisStream.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
const assert = require('assert')
const Test = require('thunk-test')
const KinesisStream = require('./KinesisStream')
const crypto = require('crypto')
const asyncIterableTake = require('./internal/asyncIterableTake')
const map = require('rubico/map')
const thunkify = require('rubico/thunkify')
const test = new Test('KinesisStream', KinesisStream)
.before(function () {
this.streams = []
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
shardIteratorType: 'TRIM_HORIZON',
getRecordsLimit: 1,
listShardsLimit: 1,
}, async function (myStream) {
await myStream.ready
await myStream.putRecord('hey')
await myStream.putRecord('ho', { partitionKey: 'ho' })
await myStream.putRecord('hi', { explicitHashKey: '127' })
const first3 = await asyncIterableTake(3)(myStream)
const first3Again = await asyncIterableTake(3)(myStream)
assert.deepEqual(first3, first3Again)
this.streams.push(myStream)
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
shardIteratorType: 'TRIM_HORIZON',
getRecordsLimit: 1,
listShardsLimit: 1,
shardCount: 2,
}, async function (myStream) {
await myStream.ready
await myStream.putRecord('hey', { partitionKey: 'a' })
await myStream.putRecord('ho', { partitionKey: 'a' })
await myStream.putRecord('hi', { partitionKey: 'a' })
const first3 = await asyncIterableTake(3)(myStream)
const first3Again = await asyncIterableTake(3)(myStream)
assert.deepEqual(first3, first3Again)
this.streams.push(myStream)
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
shardIteratorType: 'TRIM_HORIZON',
}, async function (myStream) {
await myStream.ready
await myStream.putRecords([
{ data: 'hey' },
{ data: 'ho', partitionKey: 'ho' },
{ data: 'hi', explicitHashKey: '127' },
])
const first3 = await asyncIterableTake(3)(myStream)
const first3Again = await asyncIterableTake(3)(myStream)
assert.deepEqual(first3, first3Again)
this.streams.push(myStream)
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
shardIteratorType: 'TRIM_HORIZON',
}, async function (myStream) {
myStream.kinesis.client.putRecords = () => ({
promise: async () => ({
Records: [
{
ErrorCode: 'ProvisionedThroughputExceededException',
ErrorMessage: 'Some message with accountId, stream name, and shard ID',
},
{
ErrorCode: 'ProvisionedThroughputExceededException',
ErrorMessage: 'Some message with accountId, stream name, and shard ID',
},
],
}),
})
await myStream.ready
await assert.rejects(
() => myStream.putRecords([{ data: 'hey' }]),
new AggregateError([
new Error('Some message with accountId, stream name, and shard ID'),
new Error('Some message with accountId, stream name, and shard ID'),
], 'Some records failed to process')
)
this.streams.push(myStream)
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
shardIteratorType: 'AT_TIMESTAMP',
timestamp: new Date(Date.now() - 5000),
}, async function (myStream) {
await myStream.ready
await myStream.putRecord('hey')
await myStream.putRecord('ho', { partitionKey: 'ho' })
await myStream.putRecord('hi', { explicitHashKey: '127' })
const first3 = await asyncIterableTake(3)(myStream)
const first3Again = await asyncIterableTake(3)(myStream)
assert.deepEqual(first3, first3Again)
this.streams.push(myStream)
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
}, async function (myStream) {
await myStream.ready
await myStream.putRecord('hey')
await myStream.putRecord('ho', { partitionKey: 'ho' })
await myStream.putRecord('hi', { explicitHashKey: '127' })
// subscribing after the records were put on latest, so this should hang
const latestRecordPromise = asyncIterableTake(1)(myStream)
const raceResult = await Promise.race([
latestRecordPromise,
new Promise(resolve => setTimeout(thunkify(resolve, 'hey'), 3000))
])
assert.equal(raceResult, 'hey')
myStream.close()
this.streams.push(myStream)
})
.case({
name: 'my-stream',
endpoint: 'http://localhost:4567',
shardUpdatePeriod: 500,
}, async function (myStream) {
await myStream.ready
// there shouldn't be any more records, so this should hang
const latestRecordPromise = asyncIterableTake(1)(myStream)
const raceResult = await Promise.race([
latestRecordPromise,
new Promise(resolve => setTimeout(thunkify(resolve, 'hey'), 3000))
])
assert.equal(raceResult, 'hey')
// wait a second for shard update
await new Promise(resolve => setTimeout(thunkify(resolve, 'hey'), 1000))
myStream.close()
// ensure shards don't update after close
await new Promise(resolve => setTimeout(resolve, 1000))
})
.after(async function () {
await map(async function cleanup(stream) {
stream.close()
await stream.delete()
})(this.streams)
})
if (process.argv[1] == __filename) {
test()
}
module.exports = test