-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator.spec.ts
92 lines (71 loc) · 2.94 KB
/
decorator.spec.ts
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
import { InBatches } from './decorator';
class RunInBatches {
constructor(private id: string = '') {
}
async getAll(keys: string): Promise<string>;
async getAll(keys: string[]): Promise<string[]>;
@InBatches()
async getAll(keys: string | string[]): Promise<string | string[]> {
if (Array.isArray(keys)) {
return keys.map((key, index) => `${key}-index-${index}-${this.id}`);
}
throw new Error('Should not be called with single key');
}
async getCouple(keys: string): Promise<string>;
@InBatches({ maxBatchSize: 2 })
async getCouple(keys: string | string[]): Promise<string | string[]> {
if (Array.isArray(keys)) {
return keys.map((key, index) => `${key}-index-${index}-${this.id}`);
}
throw new Error('Should not be called with single key');
}
}
describe('Batch Decorator', () => {
it('should call method in batches', async () => {
const runner = new RunInBatches('i1');
const promises = ['a', 'b', 'c'].map(key => {
return runner.getAll(key);
});
const values = await Promise.all(promises);
expect(values).toEqual(['a-index-0-i1', 'b-index-1-i1', 'c-index-2-i1']);
});
it('should call method in batches for distinct instances', async () => {
const runner1 = new RunInBatches('i1');
const runner2 = new RunInBatches('i2');
const promises1 = ['a', 'b', 'c'].map(key => {
return runner1.getAll(key);
});
const promises2 = ['d', 'e', 'f'].map(key => {
return runner2.getAll(key);
});
const values1 = await Promise.all(promises1);
expect(values1).toEqual(['a-index-0-i1', 'b-index-1-i1', 'c-index-2-i1']);
const values2 = await Promise.all(promises2);
expect(values2).toEqual(['d-index-0-i2', 'e-index-1-i2', 'f-index-2-i2']);
});
it('should call method in batches with max size', async () => {
const runner = new RunInBatches('i1');
const promises = ['batch1.1', 'batch1.2', 'batch2'].map(key => {
return runner.getCouple(key);
});
const values = await Promise.all(promises);
expect(values).toEqual(['batch1.1-index-0-i1', 'batch1.2-index-1-i1', 'batch2-index-0-i1']);
});
it('should also work when passing array of keys to batch enabled method', async () => {
const runner = new RunInBatches('i1');
const values = await runner.getAll(['a', 'b', 'c']);
expect(values).toEqual(['a-index-0-i1', 'b-index-1-i1', 'c-index-2-i1']);
});
it('should also work when passing array of keys to batch enabled method (multiple batches)', async () => {
const runner = new RunInBatches('i1');
const keys = Array.from({ length: 50 }, (_, i) => 'key-' + i);
const values = await runner.getAll(keys);
expect(values).toHaveLength(50);
// batch 1 of 25 items
expect(values).toContain('key-0-index-0-i1');
expect(values).toContain('key-24-index-24-i1');
// batch 2 of 25 items
expect(values).toContain('key-25-index-0-i1');
expect(values).toContain('key-49-index-24-i1');
});
});