-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
382 lines (295 loc) · 11.6 KB
/
index.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
373
374
375
376
377
378
379
380
381
382
import test from 'node:test'
import assert from 'node:assert/strict'
import Hold from './index.js'
import { setTimeout as sleep } from 'node:timers/promises'
test('Hold This', async (t) => {
await t.test('with a topic, set and retrieve a key and value', async (t) => {
const holder = Hold()
holder.set('myTopic', 'key', 'value')
const [result] = holder.get('myTopic', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 'value')
})
await t.test('without a topic, set and retrieve a key and value', async (t) => {
const holder = Hold()
holder.set(undefined, 'key', 'value')
const [result] = holder.get(undefined, 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 'value')
})
await t.test('key with separators creates separate columns', async (t) => {
const holder = Hold()
holder.set('multi', 'key:with:separators', 'value')
const [result] = holder.get('multi', 'key:with:separators')
assert.equal(result[0], 'key:with:separators')
assert.equal(result[1], 'value')
})
await t.test('can fetch with wildcards', async (t) => {
const holder = Hold()
holder.set('wild', 'key:abc:separators', 'value')
holder.set('wild', 'key:def:separators', 'value')
holder.set('wild', 'key:abc:others', 'value')
await t.test('key with single entry', async (t) => {
const results = holder.get('wild', '*:abc:separators')
assert.equal(results.length, 1)
assert.equal(results[0][0], 'key:abc:separators')
})
await t.test('key with multiple entries', async (t) => {
const results = holder.get('wild', 'key:*:separators')
assert.equal(results.length, 2)
assert.equal(results[0][0], 'key:abc:separators')
assert.equal(results[1][0], 'key:def:separators')
})
await t.test('key with multiple entries', async (t) => {
const results = holder.get('wild', 'key:*:*')
assert.equal(results.length, 3)
assert.equal(results[0][0], 'key:abc:others')
assert.equal(results[1][0], 'key:abc:separators')
assert.equal(results[2][0], 'key:def:separators')
})
await t.test('all keys', async (t) => {
const results = holder.get('wild', '*')
assert.equal(results.length, 3)
assert.equal(results[2][0], 'key:abc:others')
assert.equal(results[0][0], 'key:abc:separators')
assert.equal(results[1][0], 'key:def:separators')
})
})
await t.test('binding predefined topics', async (t) => {
await t.test('binding topic, set and retrieve a key and value', async (t) => {
const holder = Hold().bind('bound')
holder.set('key', 'value')
const results = holder.get('key')
assert.equal(results[0][0], 'key')
assert.equal(results[0][1], 'value')
})
await t.test('binding topic does not affect other topics', async (t) => {
const holder = Hold()
const bound = holder.bind('bound')
holder.set('not_bound', 'key', 'value')
bound.set('boundKey', 'boundValue')
const results = holder.get('not_bound', 'key')
const boundResults = bound.get('boundKey')
assert.equal(results[0][0], 'key')
assert.equal(results[0][1], 'value')
assert.equal(boundResults[0][0], 'boundKey')
assert.equal(boundResults[0][1], 'boundValue')
})
})
await t.test('serialization of non-string values', async (t) => {
await t.test('object', async (t) => {
const holder = Hold()
holder.set('object', 'key', { value: 'value' }, { isJSON: true })
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1].value, 'value')
})
await t.test('number', async (t) => {
const holder = Hold()
holder.set('object', 'key', 123456)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 123456)
})
await t.test('number', async (t) => {
const holder = Hold()
holder.set('object', 'key', 123456)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 123456)
})
await t.test('array', async (t) => {
const holder = Hold()
holder.set('object', 'key', [1, 2, 3])
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.deepEqual(result[1], [1, 2, 3])
})
await t.test('boolean', async (t) => {
const holder = Hold()
holder.set('object', 'key', true)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], true)
})
await t.test('null', async (t) => {
const holder = Hold()
holder.set('object', 'key', null)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], null)
})
await t.test('undefined', async (t) => {
const holder = Hold()
holder.set('object', 'key', undefined)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], undefined)
})
await t.test('bigint', async (t) => {
const holder = Hold()
holder.set('object', 'key', BigInt(10))
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], BigInt(10))
})
await t.test('infinity', async (t) => {
const holder = Hold()
holder.set('object', 'key', Infinity)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], Infinity)
})
await t.test('date', async (t) => {
const holder = Hold()
holder.set('object', 'key', new Date('Thu, 28 Apr 2016 22:02:17 GMT'))
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1].toISOString(), '2016-04-28T22:02:17.000Z')
})
await t.test('map', async (t) => {
const holder = Hold()
holder.set('object', 'key', new Map([['hello', 'world']]))
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1].get('hello'), 'world')
})
await t.test('set', async (t) => {
const holder = Hold()
holder.set('object', 'key', new Set([123, 456]))
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.deepEqual([...result[1]], [123, 456])
})
await t.test('regexp', async (t) => {
const holder = Hold()
holder.set('object', 'key', /([^\s]+)/g)
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1].toString(), '/([^\\s]+)/g')
})
await t.test('function', async (t) => {
const holder = Hold()
holder.set('object', 'key', function echo (arg) { return arg })
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1].toString(), 'function echo (arg) { return arg }')
})
await t.test('url', async (t) => {
const holder = Hold()
holder.set('object', 'key', new URL('https://example.com/'))
const [result] = holder.get('object', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1].toString(), 'https://example.com/')
})
})
await t.test('ttl records', async (t) => {
await t.test('returns the value of a non-expired record', async (t) => {
const holder = Hold()
holder.set('ttl', 'key', 'value', { ttl: 100 })
const [result] = holder.get('ttl', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 'value')
})
await t.test('does not return expired entries', async (t) => {
await t.test('short ttl', async (t) => {
const holder = Hold()
holder.set('ttl', 'key', 'value', { ttl: 5 })
// artificial delay
await sleep(5)
const [result] = holder.get('ttl', 'key')
assert.equal(result, undefined)
})
await t.test('zero ttl', async (t) => {
const holder = Hold()
holder.set('ttl', 'other', 'value', { ttl: 0 })
const [result] = holder.get('ttl', 'other')
assert.equal(result, undefined)
})
})
await t.test('clean expired ttl records', async (t) => {
const holder = Hold({
exposeConnection: true
})
holder.set('other', 'key', 'value', { ttl: 0 })
holder.set('ttl', 'key', 'value', { ttl: 0 })
await sleep(0)
holder.clean()
const afterCleanTTL = holder.connection.prepare('SELECT * FROM ttl').all()
const afterCleanOther = holder.connection.prepare('SELECT * FROM other').all()
const [result] = holder.get('ttl', 'key')
assert.equal(result, undefined)
assert.equal(afterCleanTTL.length, 0)
assert.equal(afterCleanOther.length, 0)
})
await t.test('clean expired ttl records for a topic', async (t) => {
const holder = Hold({
exposeConnection: true
})
holder.set('other', 'key', 'value', { ttl: 0 })
holder.set('ttl', 'key', 'value', { ttl: 0 })
await sleep(5)
holder.clean('ttl')
const afterClean = holder.connection.prepare('SELECT * FROM ttl').all()
const afterCleanOther = holder.connection.prepare('SELECT * FROM other').all()
const [result] = holder.get('ttl', 'key')
assert.equal(result, undefined)
assert.equal(afterClean.length, 0)
assert.equal(afterCleanOther.length, 1)
})
})
await t.test('bulk insert', async (t) => {
const holder = Hold({ exposeConnection: true })
holder.init('bulk', 'key', 'value')
const entries = Array.from(Array(10)).map((_, i) => holder.prepare('bulk', `key${i}`, `value${i}`))
holder.setBulk('bulk', 'key', entries)
const results = holder.connection.prepare('SELECT * FROM bulk').all()
assert.equal(results.length, 10)
})
await t.test('turbo mode', async (t) => {
await t.test('turbo mode does not interfere when not enabled', async (t) => {
const holder = Hold()
holder.set('turbo', 'key', 'value')
const [result] = holder.get('turbo', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 'value')
})
await t.test('turbo mode enabled', async (t) => {
const holder = Hold({ turbo: true })
holder.set('turbo', 'key', 'value')
const [result] = holder.get('turbo', 'key')
assert.equal(result[0], 'key')
assert.equal(result[1], 'value')
})
})
await t.test('buffered insert', async (t) => {
await t.test('threshold limit', async (t) => {
const holder = Hold({ turbo: true, bufferThreshold: 1000 })
holder.init('buffer', 'key', 'value')
for (let i = 0; i < 1000; i++) {
holder.setBuffered('buffer', 'key', `value${i}`)
}
const results = holder.get('buffer', 'key')
assert.equal(results.length, 1000)
})
await t.test('below threshold limit', async (t) => {
const holder = Hold({ turbo: true, bufferThreshold: 250 })
holder.init('below_threshold', 'key', 'value')
for (let i = 0; i < 249; i++) {
holder.setBuffered('below_threshold', 'key', `value${i}`)
}
const results = holder.get('below_threshold', 'key')
assert.equal(results.length, 0)
})
await t.test('timeout trigger', async (t) => {
const holder = Hold({ turbo: true, bufferTimeout: 50 })
holder.init('timeout', 'key', 'value')
for (let i = 0; i < 50; i++) {
holder.setBuffered('timeout', 'key', `value${i}`)
}
await sleep(50)
const results = holder.get('timeout', 'key')
assert.equal(results.length, 50)
})
})
})