-
Notifications
You must be signed in to change notification settings - Fork 67
/
BatchManager.test.ts
338 lines (301 loc) · 9.46 KB
/
BatchManager.test.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
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
import NodeCache from 'node-cache'
import type { Request } from './BatchManager'
import { BatchManager } from './BatchManager'
const mockConnection = 'https://www.goingnowhere.com'
describe('BatchManager', () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.restoreAllMocks()
})
it('should batch call into single request', async () => {
const mockBatch = createMockBatch()
const mockFetchJson = jest.fn(() => Promise.resolve([]))
const batchManager = createBatchManager(mockFetchJson)
await batchManager.batchCall(mockBatch)
expect(mockFetchJson).toHaveBeenCalledTimes(1)
expect(mockFetchJson).toHaveBeenCalledWith(mockConnection, JSON.stringify(mockBatch))
})
it('should exclude cache hits', async () => {
const mockBatch = createMockBatch()
const mockFetchJson = jest.fn().mockResolvedValue(
mockBatch.map(() => ({
result: 'cached-result',
})),
)
const batchManager = createBatchManager(mockFetchJson)
// First call
await batchManager.batchCall(mockBatch)
// Second call
await batchManager.batchCall(mockBatch)
expect(mockFetchJson).toHaveBeenCalledTimes(1)
})
it('should skip cache if cache ttl is exceeded between batches', async () => {
const mockBatch = createMockBatch()
const resolvesTo = mockBatch.map(() => ({
result: 'cached-result',
}))
const mockFetchJson = jest.fn().mockResolvedValue(resolvesTo)
const batchManager = createBatchManager(mockFetchJson)
// First call
await batchManager.batchCall(mockBatch)
const defaultCacheTtlPlus100ms = 15100
jest.advanceTimersByTime(defaultCacheTtlPlus100ms)
// Second call
await batchManager.batchCall(mockBatch)
expect(mockFetchJson).toHaveBeenCalledTimes(2)
expect(mockFetchJson).toHaveBeenLastCalledWith(mockConnection, JSON.stringify(mockBatch))
})
describe('ordering results', () => {
it('should maintain request order when served 100% via cache', async () => {
const mockBatch = createMockBatch()
const fetchJsonMockResult = mockBatch.map((requestInsideBatch) => ({
result: requestInsideBatch,
}))
const mockFetchJson = jest.fn(() => {
return new Promise((resolve) => resolve(fetchJsonMockResult))
})
const batchManager = createBatchManager(mockFetchJson)
// First call
await batchManager.batchCall(mockBatch)
// Second call (all from cache)
const batchResults = await batchManager.batchCall(mockBatch)
batchResults.forEach(function (result, index) {
expect(result.requestIdx).toBe(index)
})
expect(batchResults.map((batchResult) => batchResult.data)).toEqual(mockBatch)
})
it('should maintain the request order when all requests skip cache', async () => {
const mockBatch = createMockBatch()
const fetchJsonMockResult = mockBatch.map((requestInsideBatch) => ({
result: requestInsideBatch,
}))
const mockFetchJson = jest.fn(() => new Promise((resolve) => resolve(fetchJsonMockResult)))
const batchManager = createBatchManager(mockFetchJson)
// ACT
const batchResults = await batchManager.batchCall(mockBatch)
// ASSERT
batchResults.forEach(function (result, index) {
expect(result.requestIdx).toBe(index)
})
expect(batchResults.map((batchResult) => batchResult.data)).toEqual(mockBatch)
})
// TODO: [Mocha -> Jest] Rewrite in Jest compatible format.
it.skip('should maintain request order when served partially <100% by the cache', async () => {
// ARRANGE
const firstBatch = createMockBatch(0, 3)
const secondBatch = createMockBatch(6, 9)
const thirdBatch = createMockBatch(3, 6)
const mockFetchJson = jest
.fn()
.mockResolvedValueOnce(
firstBatch.map((requestInsideBatch) => {
return new Promise((resolve) => resolve({ result: requestInsideBatch }))
}),
)
.mockResolvedValueOnce(
secondBatch.map((requestInsideBatch) => {
return new Promise((resolve) => resolve({ result: requestInsideBatch }))
}),
)
.mockResolvedValueOnce(
thirdBatch.map((requestInsideBatch) => {
return new Promise((resolve) => resolve({ result: requestInsideBatch }))
}),
)
const batchManager = createBatchManager(mockFetchJson)
await batchManager.batchCall(firstBatch)
await batchManager.batchCall(secondBatch)
// ACT
// Final call with cached requests interwoven with uncached
const batchResults = await batchManager.batchCall([
...firstBatch,
...thirdBatch,
...secondBatch,
])
// ASSERT
batchResults.forEach(function (result, index) {
expect(result.requestIdx).toBe(index)
})
expect(batchResults.map((batchResult) => batchResult.data)).toEqual([
...firstBatch,
...thirdBatch,
...secondBatch,
])
})
})
})
function createBatchManager(mockFetchJson: any) {
const mockConstructorProps = {
url: mockConnection,
cache: new NodeCache({ stdTTL: 15 }),
}
return new BatchManager(mockConstructorProps.url, mockConstructorProps.cache, {
fetchJsonFn: mockFetchJson,
debug: false,
})
}
function createMockBatch(startSliceIndex: number = 0, endSliceIndex: number = 9): Request[] {
if (startSliceIndex < 0 || startSliceIndex >= 9) {
throw new Error(`Start Index must be greater than or equal to zero and less than 9`)
}
if (endSliceIndex < 1 || endSliceIndex > 9) {
throw new Error(`End Index must be greater than zero and less than or equal to 9`)
}
return [
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x0d8775f648430679a709e98d2b0cb6250d2887ef',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 209,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 210,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 211,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x0000000000085d4780b73119b644ae5ecd22b376',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 212,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0xe41d2489571d322189246dafa5ebde1f4699f498',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 213,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0xdd974d5c2e2928dea5f71b9825b8b646686bd200',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 214,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x0f5d2fb29fb7d3cfee444a200298f468908cc942',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 215,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0xdac17f958d2ee523a2206206994597c13d831ec7',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 216,
jsonrpc: '2.0',
},
{
method: 'eth_call',
params: [
{
from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
data: '0x70a08231000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x8e870d67f660d95d5be530380d0ec0bd388289e1',
},
'latest',
],
network: {
chainId: 2137,
name: 'unknown',
},
id: 217,
jsonrpc: '2.0',
},
].slice(startSliceIndex, endSliceIndex)
}