-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdk.test.ts
More file actions
578 lines (497 loc) · 22.6 KB
/
sdk.test.ts
File metadata and controls
578 lines (497 loc) · 22.6 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import { create } from '@bufbuild/protobuf';
import { Client } from '../src/client';
import { StoreKeyPrefix, StoreWriteBatch, TraversalMode } from '../src/store';
import {
ReduceParamsSchema,
RangeReducerSpecSchema,
RangeReduceOp,
KvExprSchema,
KvFieldRefSchema,
KvFieldRef_ValueFieldSchema,
KvFieldKind,
PolicySchema,
KeysScopeSchema,
MatchKeySchema,
PolicyGroupBySchema,
PolicyOrderBySchema,
PolicyRetainSchema,
RetainKeepLatestSchema,
PolicyOrderEncoding,
} from '../src/index';
const MAX_KEY_LEN = 254;
const tempDir = path.join(os.tmpdir(), 'exoware-ts-sdk-tests');
const configFile = path.join(tempDir, 'config.json');
function encodeStoredRowInt64(value: bigint): Uint8Array {
const buf = new ArrayBuffer(11);
const view = new DataView(buf);
view.setUint8(0, 0x01);
view.setUint8(1, 0x01);
view.setUint8(2, 0x00);
view.setBigInt64(3, value, false);
return new Uint8Array(buf);
}
function makeStreamMatchKey(prefix: string) {
return create(MatchKeySchema, {
reservedBits: 0,
prefix: 0,
payloadRegex: `(?s-u)^${prefix}.*$`,
});
}
async function nextWithTimeout<T>(
iterator: AsyncIterator<T>,
timeoutMs: number = 5_000,
): Promise<IteratorResult<T>> {
let timeout: NodeJS.Timeout | undefined;
try {
return await Promise.race([
iterator.next(),
new Promise<IteratorResult<T>>((_, reject) => {
timeout = setTimeout(() => reject(new Error('timed out waiting for stream')), timeoutMs);
}),
]);
} finally {
if (timeout) {
clearTimeout(timeout);
}
}
}
describe('Exoware TS SDK', () => {
let client: Client;
beforeAll(() => {
const config = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
const baseUrl = `http://127.0.0.1:${config.port}`;
client = new Client(baseUrl);
});
describe('StoreClient', () => {
it('should set and get a value', async () => {
const store = client.store();
const key = new TextEncoder().encode('test-key');
const value = Buffer.from('test-value');
await store.set(key, value);
const result = await store.get(key);
expect(result).not.toBeNull();
expect(Buffer.from(result!.value)).toEqual(value);
});
it('should return null for a non-existent key', async () => {
const store = client.store();
const result = await store.get(new TextEncoder().encode('non-existent-key'));
expect(result).toBeNull();
});
it('should query for key-value pairs', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'query-test-';
const pairs = [
{ key: encoder.encode(`${prefix}a`), value: Buffer.from('a') },
{ key: encoder.encode(`${prefix}b`), value: Buffer.from('b') },
{ key: encoder.encode(`${prefix}c`), value: Buffer.from('c') },
];
for (const pair of pairs) {
await store.set(pair.key, pair.value);
}
const result = await store.query(encoder.encode(`${prefix}a`), encoder.encode(`${prefix}z`));
expect(result.results.length).toBe(3);
expect(result.results.map(r => Buffer.from(r.value))).toEqual(pairs.map(p => p.value));
expect(result.results.map(r => r.key).sort()).toEqual(pairs.map(p => p.key).sort());
});
it('should setMany with multiple KVs', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'setmany-test-';
const kvs = [
{ key: encoder.encode(`${prefix}x`), value: Buffer.from('val-x') },
{ key: encoder.encode(`${prefix}y`), value: Buffer.from('val-y') },
{ key: encoder.encode(`${prefix}z`), value: Buffer.from('val-z') },
];
const sn = await store.setMany(kvs);
expect(sn).toBeGreaterThan(0n);
for (const kv of kvs) {
const result = await store.get(kv.key);
expect(result).not.toBeNull();
expect(Buffer.from(result!.value)).toEqual(kv.value);
}
});
it('should get a batch by sequence number', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'stream-getbatch-';
const kvs = [
{ key: encoder.encode(`${prefix}a`), value: Buffer.from('ba') },
{ key: encoder.encode(`${prefix}b`), value: Buffer.from('bb') },
];
const sequenceNumber = await store.setMany(kvs);
const batch = await store.getBatch(sequenceNumber);
expect(batch).not.toBeNull();
expect(batch!.sequenceNumber).toBe(sequenceNumber);
expect(batch!.entries.map((entry) => Buffer.from(entry.key))).toEqual(
kvs.map((kv) => Buffer.from(kv.key)),
);
expect(batch!.entries.map((entry) => Buffer.from(entry.value))).toEqual(
kvs.map((kv) => Buffer.from(kv.value)),
);
});
it('should return null for a missing batch sequence number', async () => {
const store = client.store();
await store.set(new TextEncoder().encode('stream-missing-sentinel'), Buffer.from('x'));
const batch = await store.getBatch(10_000_000n);
expect(batch).toBeNull();
});
it('should subscribe to replayed matching batches', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'stream-subscribe-replay-';
const kvs = [
{ key: encoder.encode(`${prefix}1`), value: Buffer.from('sa') },
{ key: encoder.encode(`${prefix}2`), value: Buffer.from('sb') },
];
const sequenceNumber = await store.setMany(kvs);
const batches = [];
for await (const batch of store.subscribe({
matchKeys: [makeStreamMatchKey(prefix)],
sinceSequenceNumber: sequenceNumber,
})) {
batches.push(batch);
break;
}
expect(batches).toHaveLength(1);
expect(batches[0].sequenceNumber).toBe(sequenceNumber);
expect(batches[0].entries.map((entry) => Buffer.from(entry.key))).toEqual(
kvs.map((kv) => Buffer.from(kv.key)),
);
expect(batches[0].entries.map((entry) => Buffer.from(entry.value))).toEqual(
kvs.map((kv) => Buffer.from(kv.value)),
);
});
it('should isolate prefixed stores and commit a cross-prefix batch', async () => {
const base = client.store();
const a = client.store(new StoreKeyPrefix(4, 1));
const b = client.store(new StoreKeyPrefix(4, 2));
const encoder = new TextEncoder();
const key = encoder.encode('prefixed-store-shared-key');
const batch = new StoreWriteBatch();
batch.push(a, key, Buffer.from('value-a'));
batch.push(b, key, Buffer.from('value-b'));
const sequenceNumber = await batch.finish(base);
const resultA = await a.get(key);
const resultB = await b.get(key);
const resultBase = await base.get(key);
expect(Buffer.from(resultA!.value)).toEqual(Buffer.from('value-a'));
expect(Buffer.from(resultB!.value)).toEqual(Buffer.from('value-b'));
expect(resultBase).toBeNull();
const rangeA = await a.query(key, key);
expect(rangeA.results).toHaveLength(1);
expect(Buffer.from(rangeA.results[0].key)).toEqual(Buffer.from(key));
expect(Buffer.from(rangeA.results[0].value)).toEqual(Buffer.from('value-a'));
const batchA = await a.getBatch(sequenceNumber);
expect(batchA).not.toBeNull();
expect(batchA!.entries).toHaveLength(1);
expect(Buffer.from(batchA!.entries[0].key)).toEqual(Buffer.from(key));
expect(Buffer.from(batchA!.entries[0].value)).toEqual(Buffer.from('value-a'));
});
it('should subscribe to logical regex matches for non-byte-aligned prefixes', async () => {
const store = client.store(new StoreKeyPrefix(4, 3));
const encoder = new TextEncoder();
const missKey = encoder.encode('prefixed-stream-miss');
const hitKey = encoder.encode('prefixed-stream-hit');
const missSequence = await store.set(missKey, Buffer.from('miss'));
const hitSequence = await store.set(hitKey, Buffer.from('hit'));
const iterator = store
.subscribe({
matchKeys: [
create(MatchKeySchema, {
reservedBits: 0,
prefix: 0,
payloadRegex: '(?s-u)^prefixed-stream-hit$',
}),
],
sinceSequenceNumber: missSequence,
})
[Symbol.asyncIterator]();
try {
const next = await nextWithTimeout(iterator);
expect(next.done).toBe(false);
expect(next.value.sequenceNumber).toBe(hitSequence);
expect(
next.value.entries.map((entry: { key: Uint8Array }) => Buffer.from(entry.key)),
).toEqual([Buffer.from(hitKey)]);
expect(
next.value.entries.map((entry: { value: Uint8Array }) =>
Buffer.from(entry.value),
),
).toEqual([Buffer.from('hit')]);
} finally {
await iterator.return?.();
}
});
it('should getMany', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'getmany-test-';
const kvs = [
{ key: encoder.encode(`${prefix}a`), value: Buffer.from('ga') },
{ key: encoder.encode(`${prefix}b`), value: Buffer.from('gb') },
];
await store.setMany(kvs);
const missingKey = encoder.encode(`${prefix}missing`);
const results = await store.getMany(
[kvs[0].key, kvs[1].key, missingKey],
);
expect(results.length).toBe(3);
const byKey = new Map(results.map(r => [
new TextDecoder().decode(r.key),
r.value ? Buffer.from(r.value) : undefined,
]));
expect(byKey.get(`${prefix}a`)).toEqual(Buffer.from('ga'));
expect(byKey.get(`${prefix}b`)).toEqual(Buffer.from('gb'));
expect(byKey.get(`${prefix}missing`)).toBeUndefined();
});
it('should query with reverse mode', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'reverse-test-';
const pairs = [
{ key: encoder.encode(`${prefix}a`), value: Buffer.from('ra') },
{ key: encoder.encode(`${prefix}b`), value: Buffer.from('rb') },
{ key: encoder.encode(`${prefix}c`), value: Buffer.from('rc') },
];
for (const pair of pairs) {
await store.set(pair.key, pair.value);
}
const forward = await store.query(
encoder.encode(`${prefix}a`),
encoder.encode(`${prefix}z`),
undefined,
4096,
TraversalMode.FORWARD,
);
const reverse = await store.query(
encoder.encode(`${prefix}a`),
encoder.encode(`${prefix}z`),
undefined,
4096,
TraversalMode.REVERSE,
);
expect(forward.results.length).toBe(3);
expect(reverse.results.length).toBe(3);
expect(reverse.results.map(r => Buffer.from(r.value))).toEqual(
[...forward.results].reverse().map(r => Buffer.from(r.value)),
);
});
it('should reduce with COUNT_ALL', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'reduce-count-';
const kvs = [
{ key: encoder.encode(`${prefix}1`), value: Buffer.from('v1') },
{ key: encoder.encode(`${prefix}2`), value: Buffer.from('v2') },
{ key: encoder.encode(`${prefix}3`), value: Buffer.from('v3') },
];
await store.setMany(kvs);
const params = create(ReduceParamsSchema, {
reducers: [
create(RangeReducerSpecSchema, {
op: RangeReduceOp.COUNT_ALL,
}),
],
});
const response = await store.reduce(
encoder.encode(`${prefix}1`),
encoder.encode(`${prefix}z`),
params,
);
expect(response.results.length).toBe(1);
const countValue = response.results[0].value;
expect(countValue).toBeDefined();
expect(countValue!.value.case).toBe('uint64Value');
expect(countValue!.value.value).toBe(3n);
});
it('should reduce with SUM on a value field', async () => {
const store = client.store();
const encoder = new TextEncoder();
const prefix = 'reduce-sum-';
const kvs = [1, 2, 3].map((n) => ({
key: encoder.encode(`${prefix}${n}`),
value: encodeStoredRowInt64(BigInt(n * 10)),
}));
await store.setMany(kvs);
const params = create(ReduceParamsSchema, {
reducers: [
create(RangeReducerSpecSchema, {
op: RangeReduceOp.SUM_FIELD,
expr: create(KvExprSchema, {
expr: {
case: 'field',
value: create(KvFieldRefSchema, {
field: {
case: 'value',
value: create(KvFieldRef_ValueFieldSchema, {
index: 0,
kind: KvFieldKind.INT64,
}),
},
}),
},
}),
}),
],
});
const response = await store.reduce(
encoder.encode(`${prefix}1`),
encoder.encode(`${prefix}z`),
params,
);
expect(response.results.length).toBe(1);
const sumValue = response.results[0].value;
expect(sumValue).toBeDefined();
expect(sumValue!.value.case).toBe('int64Value');
expect(sumValue!.value.value).toBe(60n);
});
it('should seed a serializable session from the first successful read', async () => {
const store = client.store();
const encoder = new TextEncoder();
const firstKey = encoder.encode('serializable-session-1');
const secondKey = encoder.encode('serializable-session-2');
const sn1 = await store.set(firstKey, Buffer.from('v1'));
const session = store.createSession();
expect(session.fixedSequence()).toBeUndefined();
const first = await session.get(firstKey);
expect(first).not.toBeNull();
expect(Buffer.from(first!.value)).toEqual(Buffer.from('v1'));
expect(session.fixedSequence()).toBe(sn1);
const sn2 = await store.set(secondKey, Buffer.from('v2'));
expect(sn2).toBeGreaterThan(sn1);
const second = await session.get(secondKey);
expect(second).not.toBeNull();
expect(Buffer.from(second!.value)).toEqual(Buffer.from('v2'));
expect(session.fixedSequence()).toBe(sn1);
});
it('should honor an explicit serializable session floor', async () => {
const store = client.store();
const session = store.createSessionWithSequence(10_000_000n);
expect(session.fixedSequence()).toBe(10_000_000n);
await expect(
session.get(new TextEncoder().encode('serializable-floor-test')),
).rejects.toMatchObject({
name: 'HttpError',
status: 409,
});
expect(session.fixedSequence()).toBe(10_000_000n);
});
describe('retry config', () => {
it('should accept retry config in client constructor', () => {
const c = new Client('http://localhost:1234', {
retry: { maxAttempts: 5, initialBackoffMs: 200, maxBackoffMs: 5000 },
});
expect(c.retryConfig.maxAttempts).toBe(5);
expect(c.retryConfig.initialBackoffMs).toBe(200);
expect(c.retryConfig.maxBackoffMs).toBe(5000);
});
it('should use default retry config when not specified', () => {
const c = new Client('http://localhost:1234');
expect(c.retryConfig.maxAttempts).toBe(3);
expect(c.retryConfig.initialBackoffMs).toBe(100);
expect(c.retryConfig.maxBackoffMs).toBe(2000);
});
});
it('should prune keys per KeepLatest policy', async () => {
const store = client.store();
const encoder = new TextEncoder();
function makePruneKey(group: string, version: bigint): Uint8Array {
const groupBytes = encoder.encode(group);
const prefix = encoder.encode('prune-test-');
const separator = new Uint8Array([0x00, 0x00]);
const versionBytes = new Uint8Array(8);
new DataView(versionBytes.buffer).setBigUint64(0, version, false);
const key = new Uint8Array(prefix.length + groupBytes.length + separator.length + versionBytes.length);
key.set(prefix, 0);
key.set(groupBytes, prefix.length);
key.set(separator, prefix.length + groupBytes.length);
key.set(versionBytes, prefix.length + groupBytes.length + separator.length);
return key;
}
const alphaV1 = makePruneKey('alpha', 1n);
const alphaV2 = makePruneKey('alpha', 2n);
const alphaV3 = makePruneKey('alpha', 3n);
const betaV1 = makePruneKey('beta', 1n);
const betaV2 = makePruneKey('beta', 2n);
await store.setMany([
{ key: alphaV1, value: Buffer.from('a1') },
{ key: alphaV2, value: Buffer.from('a2') },
{ key: alphaV3, value: Buffer.from('a3') },
{ key: betaV1, value: Buffer.from('b1') },
{ key: betaV2, value: Buffer.from('b2') },
]);
for (const k of [alphaV1, alphaV2, alphaV3, betaV1, betaV2]) {
expect(await store.get(k)).not.toBeNull();
}
const policy = create(PolicySchema, {
scope: {
case: 'keys',
value: create(KeysScopeSchema, {
matchKey: create(MatchKeySchema, {
reservedBits: 0,
prefix: 0,
payloadRegex:
'(?s-u)^prune-test-(?P<group>[a-z]+)\\x00\\x00(?P<version>.{8})$',
}),
groupBy: create(PolicyGroupBySchema, {
captureGroups: ['group'],
}),
orderBy: create(PolicyOrderBySchema, {
captureGroup: 'version',
encoding: PolicyOrderEncoding.U64_BE,
}),
}),
},
retain: create(PolicyRetainSchema, {
kind: {
case: 'keepLatest',
value: create(RetainKeepLatestSchema, { count: 1n }),
},
}),
});
await store.prune([policy]);
expect(await store.get(alphaV1)).toBeNull();
expect(await store.get(alphaV2)).toBeNull();
const a3 = await store.get(alphaV3);
expect(a3).not.toBeNull();
expect(Buffer.from(a3!.value)).toEqual(Buffer.from('a3'));
expect(await store.get(betaV1)).toBeNull();
const b2 = await store.get(betaV2);
expect(b2).not.toBeNull();
expect(Buffer.from(b2!.value)).toEqual(Buffer.from('b2'));
});
describe('limits', () => {
it('should handle key at size limit', async () => {
const store = client.store();
const key = new TextEncoder().encode('a'.repeat(MAX_KEY_LEN));
const value = Buffer.from('test-value');
await store.set(key, value);
const result = await store.get(key);
expect(result).not.toBeNull();
expect(Buffer.from(result!.value)).toEqual(value);
});
it('should reject key over size limit', async () => {
const store = client.store();
const key = new TextEncoder().encode('a'.repeat(MAX_KEY_LEN + 1));
const value = Buffer.from('test-value');
await expect(store.set(key, value)).rejects.toMatchObject({
name: 'HttpError',
status: 400,
});
});
it('should handle large values', async () => {
const store = client.store();
const key = new TextEncoder().encode('large_value');
const value = Buffer.alloc(128 * 1024);
await store.set(key, value);
const result = await store.get(key);
expect(result).not.toBeNull();
expect(new Uint8Array(result!.value)).toEqual(new Uint8Array(value));
});
});
});
});