-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
268 lines (221 loc) · 7.9 KB
/
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
const test = require('tape')
const SRWCache = require('./')
const MAX_AGE = 1 * 1000 // 1 second
const STALE_WHILE_REVALIDATE = 5 * 1000 // 5 seconds
const TIME_TO_VALIDATE = 1 * 1000 // 1 second
const key = 'foobar'
const params = { hello: 'world' }
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: async params => {
await new Promise(resolve => setTimeout(resolve, TIME_TO_VALIDATE))
return {
n: 42
}
}
})
test('First hit', async t => {
t.plan(2)
const start = Date.now()
const value = await get({ key, params })
const time = Date.now() - start
t.ok(time > 500, 'First hit takes more than 500ms to complete')
t.equals(value.n, 42, 'Cache has correct stored value')
})
test('Second hit - from cache', async t => {
t.plan(2)
const start = Date.now()
const value = await get({ key, params })
const time = Date.now() - start
t.ok(time < 50, 'Second hit is from cache and takes less than 50ms to complete')
t.equals(value.n, 42, 'Cache has correct stored value')
})
test('Third hit - Is stale, but quickly returned', async t => {
t.plan(2)
await new Promise(resolve => setTimeout(resolve, MAX_AGE + 100)) // Wait MAX_AGE plus 100 ms
const start = Date.now()
const value = await get({ key, params })
const time = Date.now() - start
t.ok(time < 50, 'Third hit is stale, but from cache, and takes less than 50ms to complete')
t.equals(value.n, 42, 'Cache has correct stored value')
})
test('Fourth hit - Is stale, and too old to validate. Cache should not be used, and then take more than 500ms to complete', async t => {
t.plan(2)
await new Promise(resolve => setTimeout(resolve, MAX_AGE + STALE_WHILE_REVALIDATE + TIME_TO_VALIDATE + 100)) // Wait MAX_AGE + STALE_WHILE_REVALIDATE + TIME_TO_VALIDATE plus 100 ms
const start = Date.now()
const value = await get({ key, params })
const time = Date.now() - start
t.ok(time > 500, 'Fourth hit is stale and too old to validate, so should take more than 500ms to complete')
t.equals(value.n, 42, 'Cache has correct stored value')
})
test('Allow caching of undefined values', async t => {
t.plan(4)
let calls = 0
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: () => {
calls += 1
return undefined
}
})
const firstValue = await get({ key, params })
t.equals(firstValue, undefined, 'First returned undefined')
t.equals(calls, 1, 'Was called once')
const secondValue = await get({ key, params })
t.equals(secondValue, undefined, 'Returned value is still undefined')
t.equals(calls, 1, 'Was not called again, and used cached value')
})
test('Throws in validation method should throw in get method', async t => {
t.plan(1)
const errorMessage = 'some error text'
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: () => {
throw new Error(errorMessage)
}
})
try {
await get({ key, params })
t.fail('This line should not be executed, as the validation throws an error')
} catch (err) {
t.equals(err.message, errorMessage, 'Error thrown had correct message')
}
})
test('Promise rejection in validation method should throw in get method', async t => {
t.plan(1)
const errorMessage = 'Something was unavailable'
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: () => new Promise((resolve, reject) => setTimeout(() => reject(new Error(errorMessage)), 100))
})
try {
await get({ key, params })
t.fail('This line should not be executed, as the validation throws an error')
} catch (err) {
t.equals(err.message, errorMessage, 'Error thrown had correct message')
}
})
test('If there are several pending validate promises, they should all reject', async t => {
t.plan(4)
const errorMessage = 'Something was unavailable'
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: () => new Promise((resolve, reject) => setTimeout(() => reject(new Error(errorMessage)), 100))
})
asyncGet()
asyncGet()
asyncGet()
asyncGet()
async function asyncGet () {
try {
await get({ key, params })
t.fail('This line should not be executed, as the validation throws an error')
} catch (err) {
t.equals(err.message, errorMessage, 'Error thrown had correct message')
}
}
})
test('Some async thing', async t => {
t.plan(1)
const errorMessage = 'Some async error'
async function someErrornousAsyncMethod () {
return new Promise((resolve, reject) => setTimeout(() => reject(new Error(errorMessage))))
}
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: async () => {
await someErrornousAsyncMethod()
return 'This should never be returned as the previous line rejects'
}
})
try {
await get({ key, params })
t.fail('This line should not be executed, as the validation throws an error')
} catch (err) {
t.equals(err.message, errorMessage, 'Error thrown had correct message')
}
})
test('Validation method should be re-called after errors', async t => {
t.plan(6)
let validationCalls = 0
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: async () => {
validationCalls += 1
return new Promise((resolve, reject) => setTimeout(reject, TIME_TO_VALIDATE))
}
})
asyncGet(1)
asyncGet(1)
asyncGet(1)
// Wait until after the first validation method has cleared, then call again
setTimeout(() => {
asyncGet(2)
asyncGet(2)
asyncGet(2)
}, TIME_TO_VALIDATE + 100)
async function asyncGet (validationCallsExpected) {
try {
await get({ key, params })
t.fail('This line should not be executed, as the validation throws an error')
} catch (err) {
t.equals(validationCallsExpected, validationCalls, 'Had the correct number of validation calls')
}
}
})
test('Validation call changes state (simulates unstable server)', async t => {
t.plan(12)
let validationCalls = 0
const okValue = 'The call returned a positive value'
const get = SRWCache({
maxAge: MAX_AGE,
staleWhileRevalidate: STALE_WHILE_REVALIDATE,
validate: async () => {
validationCalls += 1
if (validationCalls === 1) return new Promise((resolve, reject) => setTimeout(reject, TIME_TO_VALIDATE))
if (validationCalls === 2) return new Promise((resolve, reject) => setTimeout(() => resolve(okValue), TIME_TO_VALIDATE))
if (validationCalls === 3) return new Promise((resolve, reject) => setTimeout(reject, TIME_TO_VALIDATE))
if (validationCalls === 4) return new Promise((resolve, reject) => setTimeout(() => resolve(okValue), TIME_TO_VALIDATE))
}
})
asyncErr(1)
asyncErr(1)
// Wait until first call is done
setTimeout(() => {
asyncOk(2)
asyncOk(2)
}, TIME_TO_VALIDATE + 100)
// Wait until first two calls are done, and cache is removed
setTimeout(() => {
asyncErr(3)
asyncErr(3)
}, MAX_AGE + STALE_WHILE_REVALIDATE + 2 * TIME_TO_VALIDATE + 300)
setTimeout(() => {
asyncOk(4)
asyncOk(4)
}, MAX_AGE + STALE_WHILE_REVALIDATE + 3 * TIME_TO_VALIDATE + 500)
async function asyncErr (validationCallsExpected) {
try {
await get({ key, params })
t.fail('This line should not be executed, as the validation throws an error')
} catch (err) {
t.equals(validationCallsExpected, validationCalls, 'Had the correct number of validation calls')
}
}
async function asyncOk (validationCallsExpected) {
try {
const res = await get({ key, params })
t.equals(res, okValue, 'Validation method returned correct value')
t.equals(validationCallsExpected, validationCalls, 'Had the correct number of validation calls')
} catch (err) {
t.fail('This line should not be executed, as the validation throws an error')
}
}
})