This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
forked from mhart/kinesalite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputRecord.js
306 lines (259 loc) · 15 KB
/
putRecord.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
var BigNumber = require('bignumber.js'),
helpers = require('./helpers')
var target = 'PutRecord',
request = helpers.request,
randomName = helpers.randomName,
opts = helpers.opts.bind(null, target),
assertType = helpers.assertType.bind(null, target),
assertValidation = helpers.assertValidation.bind(null, target),
assertNotFound = helpers.assertNotFound.bind(null, target),
assertInternalFailure = helpers.assertInternalFailure.bind(null, target),
assertInvalidArgument = helpers.assertInvalidArgument.bind(null, target)
describe('putRecord ', function() {
describe('serializations', function() {
it('should return SerializationException when Data is not a Blob', function(done) {
assertType('Data', 'Blob', done)
})
it('should return SerializationException when ExplicitHashKey is not a String', function(done) {
assertType('ExplicitHashKey', 'String', done)
})
it('should return SerializationException when PartitionKey is not a String', function(done) {
assertType('PartitionKey', 'String', done)
})
it('should return SerializationException when SequenceNumberForOrdering is not a String', function(done) {
assertType('SequenceNumberForOrdering', 'String', done)
})
it('should return SerializationException when StreamName is not a String', function(done) {
assertType('StreamName', 'String', done)
})
})
describe('validations', function() {
it('should return ValidationException for no StreamName', function(done) {
assertValidation({}, [
'Value null at \'partitionKey\' failed to satisfy constraint: ' +
'Member must not be null',
'Value null at \'data\' failed to satisfy constraint: ' +
'Member must not be null',
'Value null at \'streamName\' failed to satisfy constraint: ' +
'Member must not be null',
], done)
})
it('should return ValidationException for empty StreamName', function(done) {
assertValidation({StreamName: '', PartitionKey: '', Data: '', ExplicitHashKey: '', SequenceNumberForOrdering: ''}, [
'Value \'\' at \'sequenceNumberForOrdering\' failed to satisfy constraint: ' +
'Member must satisfy regular expression pattern: 0|([1-9]\\d{0,128})',
'Value \'\' at \'partitionKey\' failed to satisfy constraint: ' +
'Member must have length greater than or equal to 1',
'Value \'\' at \'explicitHashKey\' failed to satisfy constraint: ' +
'Member must satisfy regular expression pattern: 0|([1-9]\\d{0,38})',
'Value \'\' at \'streamName\' failed to satisfy constraint: ' +
'Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+',
'Value \'\' at \'streamName\' failed to satisfy constraint: ' +
'Member must have length greater than or equal to 1',
], done)
})
it('should return ValidationException for long StreamName', function(done) {
var name = new Array(129 + 1).join('a'), name2 = new Array(257 + 1).join('a'),
data = new Buffer(1048577).toString('base64')
assertValidation({StreamName: name, PartitionKey: name2, Data: data, ExplicitHashKey: ''}, [
'Value \'' + name2 + '\' at \'partitionKey\' failed to satisfy constraint: ' +
'Member must have length less than or equal to 256',
'Value \'\' at \'explicitHashKey\' failed to satisfy constraint: ' +
'Member must satisfy regular expression pattern: 0|([1-9]\\d{0,38})',
'Value \'java.nio.HeapByteBuffer[pos=0 lim=1048577 cap=1048577]\' at \'data\' failed to satisfy constraint: ' +
'Member must have length less than or equal to 1048576',
'Value \'' + name + '\' at \'streamName\' failed to satisfy constraint: ' +
'Member must have length less than or equal to 128',
], done)
})
it('should return ResourceNotFoundException if stream does not exist', function(done) {
var name1 = randomName(), name2 = helpers.randomString()
assertNotFound({StreamName: name1, PartitionKey: name2, Data: ''},
'Stream ' + name1 + ' under account ' + helpers.awsAccountId + ' not found.', done)
})
it('should return InvalidArgumentException for out of bounds ExplicitHashKey', function(done) {
var hashKey = new BigNumber(2).pow(128).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', ExplicitHashKey: hashKey},
'Invalid ExplicitHashKey. ExplicitHashKey must be in the range: [0, 2^128-1]. Specified value was ' + hashKey, done)
})
it('should return InvalidArgumentException for version 0 in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('20000000000000000000000000000000000000000000000', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
it('should return InvalidArgumentException for version 4 in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('20000000000000000000000000000000000000000000004', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
it('should return InvalidArgumentException for version 3 in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('20000000000000000000000000000000000000000000003', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
it('should return InvalidArgumentException for initial 3 in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('30000000000000000000000000000000000000000000001', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
it('should return InvalidArgumentException for initial 1 in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('1ffffffffff7fffffffffffffff000000000007fffffff2', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
it('should return InvalidArgumentException for 8 in index in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('20000000000800000000000000000000000000000000002', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
// Not really sure that this is necessary - seems obscure
it.skip('should return InternalFailure for 8 and real time in SequenceNumberForOrdering', function(done) {
var seq = new BigNumber('2ffffffffff7fffffffffffffff000' + Math.floor(Date.now() / 1000).toString(16) + '7fffffff2', 16).toFixed()
assertInternalFailure({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq}, done)
})
it('should return InvalidArgumentException if using sequence number with large date', function(done) {
var seq = new BigNumber('13bb2cc3d80000000000000000000000', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber 26227199374822427428162556223570313216 used in PutRecord on stream ' +
helpers.testStream + ' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
it('should return InvalidArgumentException with SequenceNumberForOrdering all f', function(done) {
var seq = new BigNumber('2ffffffffff7fffffffffffffff000000000007fffffff2', 16).toFixed()
assertInvalidArgument({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq},
'ExclusiveMinimumSequenceNumber ' + seq + ' used in PutRecord on stream ' + helpers.testStream +
' under account ' + helpers.awsAccountId + ' is invalid.', done)
})
})
describe('functionality', function() {
it('should work with empty Data', function(done) {
var now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: ''}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 0, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000000'})
done()
})
})
it('should work with large Data', function(done) {
var now = Date.now(), data = new Buffer(51200).toString('base64')
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: data}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 0, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000000'})
done()
})
})
it('should work with final ExplicitHashKey', function(done) {
var hashKey = new BigNumber(2).pow(128).minus(1).toFixed(), now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', ExplicitHashKey: hashKey}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 2, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000002'})
done()
})
})
it('should work with ExplicitHashKey just below range', function(done) {
var hashKey = new BigNumber(2).pow(128).div(3).integerValue(BigNumber.ROUND_FLOOR).times(2).minus(1).toFixed(), now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', ExplicitHashKey: hashKey}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 1, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000001'})
done()
})
})
it('should work with ExplicitHashKey just above range', function(done) {
var hashKey = new BigNumber(2).pow(128).div(3).integerValue(BigNumber.ROUND_FLOOR).times(2).toFixed(), now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', ExplicitHashKey: hashKey}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 2, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000002'})
done()
})
})
it('should work with SequenceNumberForOrdering all 0', function(done) {
var seq = new BigNumber('20000000000000000000000000000000000000000000002', 16).toFixed(), now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 0, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000000'})
done()
})
})
it('should work with SequenceNumberForOrdering all 0 with version 1', function(done) {
var seq = new BigNumber('20000000000000000000000000000000000000000000001', 16).toFixed(), now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 0, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000000'})
done()
})
})
it('should work with SequenceNumberForOrdering if 8 near end', function(done) {
var seq = new BigNumber('20000000000000000000000000000000000000800000002', 16).toFixed()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: '', SequenceNumberForOrdering: seq}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
helpers.assertSequenceNumber(res.body.SequenceNumber, 0, 0)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000000'})
done()
})
})
it('should safely put concurrent, sequential records', function(done) {
this.timeout(100000)
var remaining = 100, seqIxs = []
function putRecords() {
var now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'a', Data: ''}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
seqIxs.push(parseInt(new BigNumber(res.body.SequenceNumber).toString(16).slice(11, 27), 16))
helpers.assertSequenceNumber(res.body.SequenceNumber, 0, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000000'})
now = Date.now()
request(opts({StreamName: helpers.testStream, PartitionKey: 'b', Data: ''}), function(err, res) {
if (err) return done(err)
res.statusCode.should.equal(200)
seqIxs.push(parseInt(new BigNumber(res.body.SequenceNumber).toString(16).slice(11, 27), 16))
helpers.assertSequenceNumber(res.body.SequenceNumber, 1, now)
delete res.body.SequenceNumber
res.body.should.eql({ShardId: 'shardId-000000000001'})
if (!--remaining) checkIxs()
})
})
}
function checkIxs() {
seqIxs.sort(function(a, b) { return a - b })
for (var j = 1; j < seqIxs.length; j++) {
var diff = seqIxs[j] - seqIxs[j - 1]
diff.should.equal(1)
}
done()
}
for (var i = 0; i < remaining; i++) {
putRecords()
}
})
})
})